Add links like rel=prev and rel=next to Magento’s different paginations like category, category sitemap and product sitemap.

Add code below to "page/html/head.phtml". I’ve added code at the bottom of file.

<?php
    $actionName = $this->getAction()->getFullActionName();

    if ($actionName == 'catalog_category_view' || $actionName == 'catalog_seo_sitemap_category' || $actionName == 'catalog_seo_sitemap_product')
    {
        if($actionName == 'catalog_category_view'){
            $category = Mage::registry('current_category');
            $collection = $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)));
        }
        elseif($actionName == 'catalog_seo_sitemap_category') {
            $cs = $this->getLayout()->createBlock('catalog/seo_sitemap_category');
            $collection = $cs->getCollection();
        }
        elseif($actionName == 'catalog_seo_sitemap_product') {
            $ps = $this->getLayout()->createBlock('catalog/seo_sitemap_product');
            $collection = $ps->getCollection();
        }


        $tool = $this->getLayout()->createBlock('page/html_pager')->setLimit($this->getLayout()->createBlock('catalog/product_list_toolbar')->getLimit())->setCollection($collection);

        $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 if you are on page 3.

    <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>

No comments:

Post a Comment