Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pagination with rel=next and rel=prev WordPress

  • SOLVED

Hello,

I have to admit I've copied and pasted this from [[LINK href="http://wordpress.stackexchange.com/questions/36800/adding-rel-next-rel-prev-for-paginated-archives"]]Stack Exchange[[/LINK]] where I've asked this same question before:

Does someone perhaps know a plugin or snippet (besides Yoast's WordPress SEO) to accomplish this perhaps? [[LINK href="http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html"]]Pagination with rel="next" and rel="prev"[[/LINK]]

The only thing I've come across seems to be an 3-month old [[LINK href="https://core.trac.wordpress.org/ticket/18660"]]Trac[[/LINK]] ticket.

The only bits I'm really interested in is the WordPress front page (no comment-areas or categories, etc. are required).

Also, I prefer to simply remove adjacent_posts_rel_link_wp_head and build something new instead. Not to filter it in any way.

Answers (3)

2011-12-18

Francisco Javier Carazo Gil answers:

It's important to give extra semantics to pages, so rel="prev" and rel="next" is a good option.

Have you tried this function?


function next_prev(){
global $wp_query;
$valor_alto = 999999999;
$paginacion_completa=paginate_links(
array(
'base' => str_replace( $valor_alto, '%#%', get_pagenum_link( $valor_alto ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' =>$wp_query->max_num_pages)
);
$array_paginacion=explode("</a>",$paginacion);
foreach ($array_paginacion as $enlace)
{
if (strrpos($enlace,"Siguiente"))
{
preg_match('(http://"?.*")',$enlace,$matches,PREG_OFFSET_CAPTURE,3);
$siguiente=explode('"',$matches[0][0]);
echo "<link rel=\"next\" href=\"".$siguiente[0]."\"/>\n";
}
if (strrpos($enlace,"Anterior"))
{
preg_match('(http://"?.*")',$enlace,$matches,PREG_OFFSET_CAPTURE,3);
$anterior=explode('"',$matches[0][0]);
echo "<link rel=\"prev\" href=\"".$anterior[0]."\"/>\n";
}
}
}


Variable names are in Spanish, if you want I translate it, I can do.


cor comments:

Hello Francisco,

That's OK. Normally I don't mind (also being Dutch myself).
I have some difficulties though understanding your code. Could you tell what it exactly does? (I assume you've read the Google article I've linked to?)


Francisco Javier Carazo Gil comments:

Hi Cor,

This method is an implementation for: http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html in WordPress.

Exactly, this function obtains a complete pagination if we are in home, in a category or in a tag.

Then it splits previous and next URL of current page, to insert it in HTML header into "link" tag with his attribute: "rel = prev/next".

I'm going to translate vars to English to help understanding. I have solved also an error.


<?php
function next_prev()
{
global $wp_query;

$top_level = 999999999;
$complete_pagination = paginate_links(
array(
'base' => str_replace( $top_level, '%#%', get_pagenum_link( $top_level ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' =>$wp_query->max_num_pages)
);

$array_pagination=explode("</a>",$complete_pagination);

foreach ($array_pagination as $link)
{
if (strrpos($link,"Next"))
{
preg_match('(http://"?.*")',$link,$matches,PREG_OFFSET_CAPTURE,3);
$next = explode('"',$matches[0][0]);
echo "<link rel=\"next\" href=\"" . $next[0] . "\"/>\n";
}
if (strrpos($link,"Previous"))
{
preg_match('(http://"?.*")',$link,$matches,PREG_OFFSET_CAPTURE,3);
$previous = explode('"',$matches[0][0]);
echo "<link rel=\"prev\" href=\"".$previous[0]."\"/>\n";
}
}
}
?>


cor comments:

Hi Francisco,

It's kind of weird. It's working here klanten.24design.net/html5/, but somehow isn't working here erodesign.net/.

Both installs are pretty much default.

Do you know what might be causing this perhaps?


Francisco Javier Carazo Gil comments:

Yes, it's really strange. Try to set WP_DEBUG to true and also switch on the error reporting in PHP:

error_reporting(E_ALL);
ini_set('display_errors','On');



Francisco Javier Carazo Gil comments:

Sorry, I have answered before I finished.

Tell me if there are any error. If no error, maybe some filter is giving some incompatibility.


cor comments:

Hi Francisco,

I should have realized this sooner, but the only difference between those site was its localization.

I've also renamed some vars back to match the codex (I tend to Google my code in case I forget things :))

Here's the final result:
/**
* http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html
*/
function rel_next_prev_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
$paginate_links = paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
$array_pagination = explode("</a>", $paginate_links);
foreach ($array_pagination as $link) {
if (strrpos($link, __('Next &raquo;'))) {
preg_match('(http://"?.*")', $link, $matches, PREG_OFFSET_CAPTURE, 3);
$next = explode('"', $matches[0][0]);
echo "<link rel=\"next\" href=\"" . $next[0] . "\">\n";
}
if (strrpos($link, __('&laquo; Previous'))) {
preg_match('(http://"?.*")', $link, $matches, PREG_OFFSET_CAPTURE, 3);
$previous = explode('"', $matches[0][0]);
echo "<link rel=\"prev\" href=\"" . $previous[0] . "\">\n";
}
}
}
add_action('wp_head', 'rel_next_prev_pagination');


Thanks again!

2011-12-18

Sébastien | French WordpressDesigner answers:

not sur to understand


when you are in the page 3 you want a code like that in the <head>
<link rel="prev" href="http://www.example.com/article?story=abc&page=2" />
<link rel="next" href="http://www.example.com/article?story=abc&page=4" />


Is it ?


why do you not use adjacent_posts_rel_link_wp_head exactly ?


cor comments:

Hello Sébastien,

Yes, exactly that.
I might be overlooking things, but the reason would be it's also outputting the page-title to <link


Sébastien | French WordpressDesigner comments:

in your last comment, your code is break it seems


cor comments:

I'm sorry. Yes, exactly like your reply. The reason why I'm not using adjacent_posts_rel_link_wp_head is because it also seems to be outputting the page-title.


Sébastien | French WordpressDesigner comments:

the page title is displayed in the rel, is it what you say ?


cor comments:

Perhaps I should have left my comment on the title or adjacent_posts_rel_link_wp_head :)

To not confuse thing any further I would like to have exactly as you've mentioned in your first reply:
<link rel="prev" href="http://www.example.com/article?story=abc&page=2" />

<link rel="next" href="http://www.example.com/article?story=abc&page=4" />


Sébastien | French WordpressDesigner comments:

you need that for single, page, and home, isn'it ?


cor comments:

i have to admit I'm not entirely sure.I think so, but I also think it's somehow related to the archive


Sébastien | French WordpressDesigner comments:

what is the problem exactly with adjacent_posts_rel_link_wp_head ?


cor comments:

2 reasons:

1.) adjacent_posts_rel_link_wp_head ads the title to the link
and 2.) which is the main reason I'm asking, and also is what the Google article is all about, it isn't shown on page archives.

2011-12-18

designchemical answers:

In the index.php file:


<?php if (function_exists("pagination")) { pagination($wp_query->max_num_pages); } ?>


In the functions.php file:

/* PAGINATION */
function pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;

global $paged;
if(empty($paged)) $paged = 1;

if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}

if(1 != $pages)
{
echo "<div class=\"clear\"></div><div class=\"page-navi\">";
echo "<p>Page ".$paged." of ".$pages."</p>";
echo "<ul>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link(1)."'>&laquo; First</a></li>";
if($paged > 1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link($paged - 1)."' rel='prev'>&lsaquo; Previous</a></li>";

for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<li><a href='".get_pagenum_link($i)."' class='current'>".$i."</a></li>":"<li><a href='".get_pagenum_link($i)."' class='inactive'>".$i."</a></li>";
}
}

if ($paged < $pages && $showitems < $pages) echo "<li><a href=\"".get_pagenum_link($paged + 1)."\" rel='next'>Next &rsaquo;</a></li>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a href='".get_pagenum_link($pages)."'>Last &raquo;</a></li>";
echo "</ul></div>\n";
}
}


cor comments:

Hello designchemical,

I have to admit I haven't tried your snippet. Due to its format, and if I have to guess it's about single page navigation. At least, not something related to my original question which basically seems to happen in the document header.
(also, I have to admit I'd prefer to continue on something like this instead: https://gist.github.com/1401161)