Adding rel=“next” & rel=“prev” in WordPress

Since September 2011 you can use the html ink element rel=”next” and rel”prev” to avoid duplicate content issues in paginated pages, especially in archives and categories.

You can read more about the rel next and rel prev in the official Google Webmaster Central Blog.

Many developers and SEO specialists, included myself, have already asked WordPress developers to add this html link element within the core version of WordPress, but at the moment we haven’t received an official answer. For this reason I had to hack my function.php page to add this function. As it works perfectly well, I would like to share it with you.

Here is what you have to do:

1) Crete a backup version of your function.php page, open the version you want to modify;

2) Add this php scrip after the opening tag;

[box style="0"]

function rel_next_prev(){
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
}

}
add_action( ‘wp_head’, ‘rel_next_prev’ );

[/box]

3) Save and upload the file back into the server.

This new function will avoid your paginated pages duplicate issues in your wordpress site adding in the head section something that will look like this:

I hope you find this guide useful.

2 thoughts on “Adding rel=“next” & rel=“prev” in WordPress

  1. simleon Post author

    Hi, thanks for your comment Sebastiano. It depends what duplicate content you are referring too. Noindex is never a mandatory option… every scenario has different solutions. In the pagination pages series, the rel next and prev are a good solution… as even Google suggested.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>