I have a wordpress function that adds a query string 'nocfcache=1'
to a single page url.
function nocfcache_query_string( $url, $id ) {
if( 42 == $id ) {
$url = add_query_arg( 'nocfcache', 1, $url );
}
return $url;
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
Issue: How to use multiple page ids in the function so as to make sure they will all have the query string appended to the url.
What I have tried so far:
function nocfcache_query_string( $url, $id ) {
$id = array (399, 523, 400, 634, 636, 638);
if(in_array($post->ID, $id)) {
$url = add_query_arg( 'nocfcache', true, get_permalink( $post->ID ));
return $url;
exit;
}
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
I have a wordpress function that adds a query string 'nocfcache=1'
to a single page url.
function nocfcache_query_string( $url, $id ) {
if( 42 == $id ) {
$url = add_query_arg( 'nocfcache', 1, $url );
}
return $url;
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
Issue: How to use multiple page ids in the function so as to make sure they will all have the query string appended to the url.
What I have tried so far:
function nocfcache_query_string( $url, $id ) {
$id = array (399, 523, 400, 634, 636, 638);
if(in_array($post->ID, $id)) {
$url = add_query_arg( 'nocfcache', true, get_permalink( $post->ID ));
return $url;
exit;
}
}
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
Share
Improve this question
edited May 15, 2019 at 20:28
nmr
4,5672 gold badges17 silver badges25 bronze badges
asked May 15, 2019 at 18:51
andreasherneandreasherne
31 bronze badge
2
|
1 Answer
Reset to default 0In your second code you use undefined $post
variable. Probably you meant a global $post
, but as the second parameter to the filter is passed the ID of the post for which the link is created, and this does not necessarily have to be the current post of the loop.
Try this:
add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
function nocfcache_query_string( $url, $id )
{
$ids = array (399, 523, 400, 634, 636, 638);
if( in_array($id, $ids) )
{
$url = add_query_arg( 'nocfcache', 1, $url );
}
return $url;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745489845a4629938.html
array (399
... so it's justarray(399
... – WebElaine Commented May 15, 2019 at 19:46