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
  • That should work - try taking out the space in array (399... so it's just array(399... – WebElaine Commented May 15, 2019 at 19:46
  • @WebElaine taking out the space in array (399... didn't work, I flushed the permalinks but when I go to the edit page there is no permalink nor view page available, clicking on view (from Pages) gets me back to the wp-admin/pages. – andreasherne Commented May 15, 2019 at 20:01
Add a comment  | 

1 Answer 1

Reset to default 0

In 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