Skip to content

Instantly share code, notes, and snippets.

@jasontipton
Created November 17, 2014 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasontipton/fb62e1771280d197389d to your computer and use it in GitHub Desktop.
Save jasontipton/fb62e1771280d197389d to your computer and use it in GitHub Desktop.
rel="prev/next" - Magento 1.9 Community Edition
<?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>
<?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 . '" />';
}
?>
@jasontipton
Copy link
Author

The original phtml comes from a blog post by Inchoo.
http://inchoo.net/magento/how-to-implement-relprev-and-relnext-to-magentos-pagination/

To ensure that this block of code only renders on category pages the phtml has been moved to the following file path:
app/design/YOUR_PACKAGE/YOUR_THEME/template/page/html/rel_prev_next.phtml
and the updates to local.xml will include the template block only on category and layered category pages.

A big thank you to Sean Breeden (http://www.seanbreeden.com) for his help on how to move this into its own file.

@PatrickCronin
Copy link

I love where you're going with this. Did you have to add a $this->getChildHtml('rel_next_prev') in head.phtml to get Magento to render the block, or were the layout updates enough? The "head" block is an instance of Mage_Page_Block_Html_Head, which extends from Mage_Core_Block_Template, so I wouldn't expect that adding the "relprev.next" block as a child block would be enough to get the "relprev.next" block to render (as far as I know, only core/text_list blocks automatically render their child blocks.)

@ansarul
Copy link

ansarul commented Aug 19, 2017

Hi I am facing pagination please check attachment my source code , if anyone know about how to solve it please help i am really appreciate
Website URL is http://komal.com.au/scarves.html
screenshot_5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment