ProcessWire
Recipes_

Enhance paginator with rel attribute for SEO

Problem

Search engines want special code in the header to identify pages with a paginator correctly and there is no obvious way of implementing this with the ProcessWire paginator.

<a rel="prev" href="http://www.example.com/article?page=1" />...</a>
<a rel="next" href="http://www.example.com/article?page=3" />...</a>

Solution

$limit = 12; // the "limit"-setting used on this page
$children = $page->children("limit=" . $limit);
$totalpages = ceil($children->getTotal() / $limit);

if ($input->pageNum) {
    if ($input->pageNum < $totalpages) {
        echo "<a rel='next' href='" . $page->url . $config->pageNumUrlPrefix . ($input->pageNum + 1) . "' />";
    }
    if ($input->pageNum > 1) {
        echo "<a rel='prev' href='" . $page->url . $config->pageNumUrlPrefix . ($input->pageNum - 1) . "' />";
    }
}

// Output:
foreach($children as $child): ?>

...

<?php endforeach; ?>

<?php echo $children->renderPager(); ?>

Resources