Generating rel=prev and rel=next only on wordpress categories

I was trying to remove the rel=prev and rel=next tags from my website as my SEO gut suggested. I found those functions which perfectly do the magic:

remove_action('wp_head', 'start_post_rel_link', 10, 0 );
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

however, after a while my SEO guy suggested to leave it on a categories section where over there it’s necassary as far as SEO.

Read More

is there a simple way of implementing the rel=prev and rel=next tags
only on a categories pages?, e.g: www.website.com/category/category-name
and remove it from all other post types? (post, page, homepage etc etc)

Thanks, GIl

Related posts

Leave a Reply

3 comments

  1. Not entirely sure if I agree with the explanation of your SEO guy as rel=prev and rel=next are used for paginated archives.

    Of course categories can be one, but also your blog if it’d contain multiple pages.

    Anyways, here’s what I’m currently using:

    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
    
    add_action( 'wp_head', 'cor_rel_next_prev_pagination' );
    /**
     * Pagination with rel="next" and rel="prev".
     *
     * @link wp-includes|default-filters.php
     * @link http://core.trac.wordpress.org/ticket/18672 Implement rel="prev" and rel="next" for archives
     * @link http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html Pagination with rel="next" and rel="prev"
     */
    function cor_rel_next_prev_pagination() {
      global $paged;
      if ( get_previous_posts_link() ) {
    ?>
      <link rel="prev" href="<?php echo get_pagenum_link( $paged - 1 ); ?>">
    <?php
      }
      if ( get_next_posts_link() ) {
    ?>
      <link rel="next" href="<?php echo get_pagenum_link( $paged + 1 ); ?>">
    <?php
      }
    }
    

    Edit: also I believe start_post_rel_link is no longer being used.

  2. I think you can wrap your hooks into template_redirect hook, which is fired before sending content to browser, something like this:

    add_action( 'template_redirect', 'wpse_47638_remove_rel_link' );
    function wpse_47638_remove_rel_link() {
        if ( is_category() )
            return;
        remove_action('wp_head', 'start_post_rel_link', 10, 0 );
        remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
    }
    
  3. Try the is_category function. You should be able to add a high priority action to wp_head that checks the result, then conditionally runs the two statements you have above to remove the actions that assign REL to the links.

    I’m honestly not sure if this method is operable this early in the WP lifecycle, but failing that, grab the URL using _SERVER and test for a category pattern.