WordPress how to override function adjacent_posts_rel_link_wp_head() in link-template.php the correct way

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 Answer
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 );
} );

Leave a Comment