2

I'm quite new to WordPress development. I'm using WordPress 4.9.2. I'm currently working on the following issue. I have several blog categories which have no relation to each other. For example "Our Services" and "News". Now I want to make sure that the Prev/Next links on each post page are only pointing to Prev/Next within the same category. This should apply for links on the page and for rel links in the head section. I figured already that I JUST have to set the flag $in_same_term to true. For the links at the end of the post, I solved the problem already inside my theme by adding correct args. Only the rel links in the head section are not working like this.

Needed change for header

From

function adjacent_posts_rel_link_wp_head() {
    if ( ! is_single() || is_attachment() ) {
        return;
    }
    adjacent_posts_rel_link();
}

To

function adjacent_posts_rel_link_wp_head() {
    if ( ! is_single() || is_attachment() ) {
        return;
    }
    adjacent_posts_rel_link('%title', true); //TODO find a better way
}

I just don't want to touch a WordPress core file. Could anyone help and guide me through the correct process, please?

1

adjacent_posts_rel_link() calls get_adjacent_post_rel_link(), which runs its return value through the {$adjacent}_post_rel_link filter. The dynamic portion of the hook name, $adjacent, refers to the type of adjacency, 'next' or 'previous'.

You can use that filter to change the output accordingly. Here's an example to change the second parameter:

add_filter( 'next_post_rel_link', function( $link ) {
  return get_adjaxent_post_rel_link( '%title', true, '', false );
} );

add_filter( 'previous_post_rel_link', function( $link ) {
  return get_adjacent_post_rel_link( '%title', true );
} );
  • Thak you very much for your help but for me, this ends in an endless loop because get_adjaxent_post_rel_link is itself applying the filters. Do you have any other ideas how to solve my problem? – dj_thossi Feb 6 '18 at 7:16
  • 1
    @dj_thossi Ah, right. You'd need to run something like remove_filter( current_filter(), __FUNCTION__ ) from inside the callback to prevent the infinite loop. – swissspidy Feb 6 '18 at 10:16
  • I tried the following at the beginning of single.php inside the theme and I still have an endless loop. may anyone be able to help? add_filter( 'previous_post_rel_link', function( $link ) { remove_filter( current_filter(), FUNCTION ); return get_adjacent_post_rel_link( '%title', true, '', true ); } ); – dj_thossi Apr 14 '18 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.