Qcybb

We are here to help you solve your problems!

You are here: Home / SEO / Add rel=prev & rel=next Meta Tags in WordPress

Add rel=prev & rel=next Meta Tags in WordPress

August 21, 2012 13 Comments

It’s important to include the rel=”prev” & rel=”next” meta tags in your WordPress blog. These meta tags will show Google the relationships of documents that span over multiple pages and distinguish them as a series.

This is great for SEO and helps Google properly index your information. Proper indexing means more relevant results to users and more targeted traffic to your site.

It’s quite simple to add these meta tags to the section on your blog.

What you need to do is open your theme functions.php file and add :

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
	}
}
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
add_action('wp_head', 'cor_rel_next_prev_pagination');

To check to see if it’s working, navigate to page 2 on your blog and view the source.

You should see something like this :

<link rel="prev" href="http://example.com/">
<link rel="next" href="http://example.com/page/2/">

More information can be found on the Google Webmaster Blog.

Filed Under: SEO

Leave a Comment ↓

Comments

  1. Luis says

    July 22, 2013 at 1:31 pm

    Thanks, very helpful, I spend a lot of time searching that.

    Reply to this Comment
  2. James Clark says

    March 22, 2014 at 4:21 am

    Works like a charm on category pages and author pages too – anywhere with pagination! Thank you very much

    Reply to this Comment
  3. pedro says

    July 30, 2014 at 6:29 pm

    Thanks! Just one question: how can I make this code work for comments pagination as well?

    Reply to this Comment
  4. Lui says

    July 31, 2014 at 1:02 am

    Thanks. This is important right now with new changes from Google.

    Reply to this Comment
  5. Mido says

    May 12, 2016 at 6:12 pm

    Thanks! but I am using yoast and i am using static homepage where there is no pagination and it show up. How I can remove it from homepage.

    Reply to this Comment
  6. Dave Hannon says

    May 12, 2016 at 7:24 pm

    @Mido You’ll need to do something like this then :

    if ( !is_front_page() )
    {
    remove_action(‘wp_head’, ‘adjacent_posts_rel_link_wp_head’);
    add_action(‘wp_head’, ‘cor_rel_next_prev_pagination’);
    }

    Reply to this Comment
  7. Matthew Hepburn says

    December 27, 2016 at 9:41 am

    While the rel=”prev” seems to be working, rel=”next” does not.

    Reply to this Comment
    • Dave Hannon says

      December 27, 2016 at 10:05 am

      I see that you are using the Yoast SEO plugin – this functionality should already be a built in option (I believe you just need to enable it).

  8. Matt says

    December 27, 2016 at 11:37 am

    It is set to work, but seems to only be working with posts and categories. I have confirmed this with Toast support. I still trying switching off toast pagination and see if that fixes it.

    Reply to this Comment
    • David says

      January 25, 2017 at 11:04 am

      Hello, did you find the soulution? I’m in the same situation.

    • Matthew Hepburn says

      January 25, 2017 at 12:14 pm

      I did actually find the solution. I stopped using Yoast, as their plugin changed. For some reason they only use rel=”prev” and use a canonical link back to the first page. My problem with that is that Google created this, as many people were using a lot methods. Google specifically wanted to have this group paginated pages to be identified as part of a group of pages. Yoast solution really does that anymore. Their change is more focused on pointing to the first page.

      Here is Google’s post on what they want:

      https://webmasters.googleblog.com/2011/09/pagination-with-relnext-and-relprev.html

      To make this happen, I have added the following code to my themes functions.php file.

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

      This code was found on:

      http://wordpress.stackexchange.com/questions/36800/adding-rel-next-rel-prev-for-paginated-archives

      In addition, I am using the all in one SEO plugin. You can click NO Pagination for cannonical URLS in general setting. This adds a NO Index attribute to paginated URLS, will add a canonical link to the first instance of the page, which is set to a self-refrencing URL.

      Depending on the page structure you are using, you could also modify your robot.txt file. For instance, I was working on a client’s site, where paginated pages were in the following format

      Paginated 2nd page: /?idxpage=2

      In this instance I added a wildcard command to the robots.txt file.

      Disallow .* ?idxpage=.*

      If your URL structure is follows normal WordPress URL structure, you will not need to modify the robot.txt file.

  9. Marcos financas says

    March 18, 2020 at 8:18 pm

    Realmente material muito útil me ajudou bastante obrigado

    Reply to this Comment
    • Dave Hannon says

      March 18, 2020 at 10:12 pm

      Seja bem-vindo!

Leave a Reply to Marcos financas Cancel reply

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

Categories

  • Question2Answer
  • SEO
  • WordPress

Recent Comments

  • Dave Hannon on Comment Form Validation for WordPress
  • Hefazat on Comment Form Validation for WordPress
  • farhan on Question2Answer SEO Meta Tags
  • Dave Hannon on Question2Answer Akismet Plugin
  • mark on Question2Answer Akismet Plugin

Copyright © 2006–2025 Qcybb.com