Magento. Add rel links prev/next
into head for pagination (SEO)
<?php
class Vendor_Extension_Block_Html_Rel extends Mage_Core_Block_Template
{
/**
* Get current category
* @return Mage_Catalog_Model_Category
*/
protected function _getCategory ()
{
return Mage::registry ("current_category " );
}
/**
* Get current product collection
* @return Mage_Catalog_Model_Resource_Product_Collection | null
*/
protected function _getProductCollection ()
{
/* @var $category Mage_Catalog_Model_Category */
$ category = $ this ->_getCategory ();
if (
$ category &&
$ category ->getId ()
) {
/* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
$ collection = $ category ->getProductCollection ();
$ collection ->addAttributeToFilter ('status ' , 1 )
->addAttributeToFilter ('visibility ' , array ('in ' => array (Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG , Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH )));
return $ collection ;
}
}
/**
* Get pager
* @return Mage_Page_Block_Html_Pager
*/
public function getPager ()
{
/* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
$ collection = $ this ->_getProductCollection ();
if (
$ collection &&
$ collection ->getSize ()
) {
/* @var $pager Mage_Page_Block_Html_Pager */
$ pager = $ this ->getLayout ()->createBlock ('page/html_pager ' );
$ pager ->setLimit ($ this ->getLayout ()->createBlock ('catalog/product_list_toolbar ' )->getLimit ())
->setCollection ($ collection );
return $ pager ;
}
}
}
Create template ../app/design/frontend/package/theme/template/page/html/rel.phtml
<?php /* @see Vendor_Extension_Block_Html_Rel */ ?>
<?php /* @var $this Vendor_Extension_Block_Html_Rel */ ?>
<?php /* @var $pager Mage_Page_Block_Html_Pager */ ?>
<?php $ pager = $ this ->getPager () ?>
<?php if ($ pager ): ?>
<?php if (!$ pager ->isFirstPage ()): ?>
<link rel="prev" href="<?php echo $ pager ->getPreviousPageUrl () ?> " />
<?php endif ; ?>
<?php if (!$ pager ->isLastPage ()): ?>
<link rel="next" href="<?php echo $ pager ->getNextPageUrl () ?> " />
<?php endif ; ?>
<?php endif ;
Call template in ../app/design/frontend/package/theme/template/page/html/head.phtml
<?php echo $ this ->getLayout ()->createBlock ('extension/html_rel ' )->setTemplate ('page/html/rel.phtml ' )->toHtml ();?>