admin管理员组文章数量:1429091
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;
}
本文标签: permalinksAdding query string to multiple page urls in a Wordpress function
版权声明:本文标题:permalinks - Adding query string to multiple page urls in a Wordpress function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745489845a2660554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
array (399
... so it's justarray(399
... – WebElaine Commented May 15, 2019 at 19:46