Adding Rel="Next" and Rel="Prev" Tags to Paginated Single Posts (Using NextPage Tag)

WordPress allows you to break larger posts into smaller sections using the <!--nextpage--> tag. All you need to do is add the <!--nextpage--> tag anywhere within the article body where you want to article to break. You can add the tag at multiple places to break the article into multiple pages. This is a really useful feature, but could cause issues with duplicate content.

Even though Google has said that they are great at identifying paginated posts they have also stressed on the use of rel="prev" and rel="next" meta tags to indicate relationship between paginated pages as directed in this post.

Currently (version 4.2.2), wordpress already adds a rel="prev" and rel="next" tag to single posts, but these tags are added to all posts irrespective of whether they have pagination or not. And oddly enough, the pages that have pagination do not have these tags. Having these tags on non-paginated posts makes little to no sense from the SEO perspective and it only serves as additional clutter you can get rid of.

For the time being though, if you have paginated posts, you will need to use a function that adds these tags to paginated posts and removes them from regular non-paginated posts. You can achieve this using a custom function that adds these tags by adding an action to the wp_head or use a plugin like the Yoast SEO plugin that does a great job of removing these tags from all posts and adding them only to paginated posts and archive pages.

In this article, let's see how we can add these tags to paginated posts using a custom function.

Adding the Next and Prev Tags to Paginated Single Posts

To add these tags to your blog, copy and paste the following function to your theme's functions.php page (towards the end):

/*Add Prev and Next Tags to Paginated Single Posts*/
function add_rel_prev_next_paginated_posts(){
global $post;
$paged = intval(get_query_var('paged')); 

	$perm_link = get_permalink();

# 1: Count number of paginated pages.
if( isset( $post->post_content ) ){
	$num_pages = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;

# 2: If pagination exists add the tags.
if($num_pages > 1){
	$page = intval( get_query_var('page') ); # Get Page number
	if( $page == 0 ){ $page = 1; }
	# 3: Add rel="prev" meta tag.
	if( ($page > 1) && ($page <= $num_pages) ){
		$prev_page_num = ($page - 1);
		if($page == 2){	$prev_page_num = '';}
		$full_url = user_trailingslashit(trailingslashit($perm_link) . $prev_page_num);
		echo '<link rel="prev" href="'. esc_url($full_url) .'" />';
		echo "\n";
	# 4: Add rel="next" meta tag.
	if( ($page >= 1) && ($page < $num_pages) ){
		$nxt_page_num = ($page + 1);
		$full_url = user_trailingslashit(trailingslashit($perm_link) . $nxt_page_num);
		echo '<link rel="next" href="'. esc_url($full_url) .'" />';
		echo "\n";
add_action('wp_head', 'add_rel_prev_next_paginated_posts', 2);

Code Explanation: We first check if it's a single post page and then use PHP's substr_count function to count the occurrences of the tag <!--nextpage--> in the content. If the count returns zero, it means the post is not paginated and hence we do nothing. If the count returns a number, the post is paginated. Total paginated pages is calculated by adding 1 to the count. So if the <!--nextpage--> appears twice within the content, the total pages will be 3 - the front page, plus two paginated pages.

We then use the $page global variable (which we get using the wordpress get_query_var function) to get the current page number of the page and add the rel="prev" and rel="next" meta tags based on the page we are on.

Note: Please note that this code will only work if you are using permalinks.

Once you add the function, here's an example on how the tags should display:

Example: Let's say you have a post which has four paginated pages. This is how the tags will be displayed for these pages:

1st page: The first page will only display the rel="next" tag pointing to page 2.

<link rel="next" href="" />

2nd Page: The second page will display both tags, with the Next tag pointing to page 3 and Prev tag pointing to the main article.

<link rel="next" href="" />
<link rel="prev" href="" />

3rd Page: The third page will have the Next tag pointing to Page 4 and Prev tag pointing to Page 2.

<link rel="next" href="" />
<link rel="prev" href="" />

4th and Final page: The final page will only display the Prev tag and this will point to Page 3.

<link rel="prev" href="" />

Removing WordPress Generated Next and Prev Tags

The next step is to remove the rel='next' and rel='prev' meta tags generated by wordpress as leaving them there could lead to duplicate meta tags. To remove these wordpress generated tags, add the following function to your functions.php page:

remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');

Once you have added these functions, make sure to check the HTML source of your paginated posts to see if these tags have been added and if they are displaying the correct URLs. Also check your regular posts to see if the wordpress generated Next and Prev tags have been removed.



  1. Cool! Thank you very much for the code! I rummaged through the entire Russian-language Internet, and found it here! Question. Tags "description" and "keywords" should be removed from the pages of pagination and how to do it? Yes, and still with rel = "canonical" how to be?

    With HI. Dzmitry

    • M Mukesh says:

      Thanks Dzmitry. Yes it is possible to remove 'meta description' and 'keywords' tags from paginated pages. But I will need to have a look at the code that is generating these tags in the first place. Send me an email with the code and I will help you out. Check out my contact page for my email.

  2. I installed your code and everything works fine! Concerning a conclusion "description" and "keywords", I have sent to you on mail a code which I use at a conclusion of these tags.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.