PHP Magento Tips

How to Implement rel=prev and rel=next to Magento’s pagination?

Rate this posting:
{[['']]}

As most of you know that "When dealing with online stores with a lot of products, pagination on category pages can get really problematic for search engine optimization (SEO)". you can add rel="prev" and rel="next link tag attributes in the head tag for pages, which will boost your SEO.

Step 1: Add below code in your local.xml file.

<?xml version="1.0"?>
<layout version="0.1.0">

    <!-- add rel="prev" and rel="next" for Google SEO -->
    <catalog_category_default>
        <reference name="head">
            <block type="page/html_pager" name="relprev.next" as="rel_prev_next" template="page/html/rel_prev_next.phtml"/>
        </reference>
    </catalog_category_default>

    <catalog_category_layered>
        <reference name="head">
            <block type="page/html_pager" name="relprev.next" as="rel_prev_next" template="page/html/rel_prev_next.phtml"/>
        </reference>
    </catalog_category_layered>
    
</layout> 

Step 2: Create file in app/design/frontend/rwd/default/template/page/html/rel_prev_next.phtml and add below code in it.

<?php
/*
    code originally posted by Inchoo.net
    http://inchoo.net/magento/how-to-implement-relprev-and-relnext-to-magentos-pagination/
*/
?>
<?php
$actionName = $this->getAction()->getFullActionName();
if ($actionName == 'catalog_category_view') // Category Page
{
    $category = Mage::registry('current_category');
    $prodCol = $category->getProductCollection()->addAttributeToFilter('status', 1)
                ->addAttributeToFilter('visibility', array('in' => array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)));
    $tool = Mage::app()->getLayout()->createBlock('page/html_pager')->setLimit(Mage::app()->getLayout()->createBlock('catalog/product_list_toolbar')->getLimit())->setCollection($prodCol);
    $linkPrev = false;
    $linkNext = false;
    if ($tool->getCollection()->getSelectCountSql()) {
        if ($tool->getLastPageNum() > 1) {
            if (!$tool->isFirstPage()) {
                $linkPrev = true;
                if ($tool->getCurrentPage() == 2) {
                    $url = explode('?', $tool->getPreviousPageUrl());
                    $prevUrl = @$url[0];
                }
                else {
                    $prevUrl = $tool->getPreviousPageUrl();
                }
            }
            if (!$tool->isLastPage()) {
                $linkNext = true;
                $nextUrl = $tool->getNextPageUrl();
            }
        }
    }
    if ($linkPrev) echo '<link rel="prev" href="' . $prevUrl . '" />';
    if ($linkNext) echo '<link rel="next" href="' . $nextUrl . '" />';
}

?>

Result

Below is a result you will get if you are on page 3.

Just check source code of your page.

<head>
...
<link rel="prev" href="http://www.example.com/store.html?p=2">

<link rel="next" href="http://www.example.com/store.html?p=4">
...
</head>

Tag : Magento
0 Comments On "How to Implement rel=prev and rel=next to Magento’s pagination?"

Back To Top