php - rel="next" and rel="prev" for magento pagination

648

Hello I want to put the rel=“next” and rel=“prev” tag for category pagination into the section of my magento website. For detials on the rel=“next” and rel=“prev” tag see http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html.

I am using the code from shadowice222 posted on Magento: Put "product list pager"-block in <head>.

    <?php
    $actionName = $this->getAction()->getFullActionName();
    if($actionName == 'catalog_category_view') // Category Page
    {
        $id = Mage::app()->getRequest()->getParam('id', false); //cat id
        $category = Mage::getModel('catalog/category')->load($id);
        $prodCol = $category->getProductCollection();
        $tool = $this->getLayout()->createBlock('catalog/product_list_toolbar')->setCollection($prodCol);
        $linkPrev = false;
        $linkNext = false;
        if ($tool->getCollection()->getSize()) {
            if ($tool->getLastPageNum() > 1) {
                if (!$tool->isFirstPage()) {
                    $linkPrev = true;
                    $prevUrl = $tool->getPreviousPageUrl();
                }
                if (!$tool->isLastPage()) {
                    $linkNext = true;
                    $nextUrl = $tool->getNextPageUrl();
                }
            }
        }
        ?>
        <?php if ($linkPrev): ?>
        <link rel="prev" href="<?php echo $prevUrl ?>" />
        <?php endif; ?>
        <?php if ($linkNext): ?>
        <link rel="next" href="<?php echo $nextUrl ?>" />
        <?php endif; ?>
<?php
    }
?>

I got the problem, that on my second paginated page the is a

<link rel="prev" href="http://www.website.de/category1.html?p=1" />

This should be

<link rel="prev" href="http://www.website.de/category1.html" />

like the normal category url, which is the normal first page. Otherwise Google will be confused. Can anybody help me out in just changing the rel="prev" tag from the second to the first page. Everything else is working just fine. Thank you very much in advance.

22

Answer

Solution:

I would look at adding something into the code that sets up the previous url link:

<?php
$actionName = $this->getAction()->getFullActionName();
if($actionName == 'catalog_category_view') // Category Page
{
    $id = Mage::app()->getRequest()->getParam('id', false); //cat id
    $category = Mage::getModel('catalog/category')->load($id);
    $prodCol = $category->getProductCollection();
    $tool = $this->getLayout()->createBlock('catalog/product_list_toolbar')->setCollection($prodCol);
    $linkPrev = false;
    $linkNext = false;
    if ($tool->getCollection()->getSize()) {
        if ($tool->getLastPageNum() > 1) {
            if (!$tool->isFirstPage()) {
                $linkPrev = true;

                // check here for being on the second page
                if ($tool->getCollection->getCurPage() == 2) {
                    // set the page to the first page correctly
                    $url = explode('?', $this->helper('core/url')->getCurrentUrl());
                    $prevUrl = $url[0];
                } else {
                    // retrieve the "normal" previous url
                    $prevUrl = $tool->getPreviousPageUrl();                        
                }
            }
            if (!$tool->isLastPage()) {
                $linkNext = true;
                $nextUrl = $tool->getNextPageUrl();
            }
        }
    }
    ?>
    <?php if ($linkPrev): ?>
    <link rel="prev" href="<?php echo $prevUrl ?>" />
    <?php endif; ?>
    <?php if ($linkNext): ?>
    <link rel="next" href="<?php echo $nextUrl ?>" />
    <?php endif; ?>
<?php
    }
?>
948

Answer

Solution:

Just read a new blog on Inchoo.net -> http://inchoo.net/ecommerce/magento/how-to-implement-relprev-and-relnext-to-magentos-pagination/

They've tweaked the code a bit

instead of

$id = Mage::app()->getRequest()->getParam('id', false); //cat id
$category = Mage::getModel('catalog/category')->load($id);

they use

$category = Mage::registry('current_category');

seems a bit more clean to me.

343

Answer

Solution:

The easiest way is just remove it from the string with a simple string replace:

    <?php
    $actionName = $this->getAction()->getFullActionName();
    if($actionName == 'catalog_category_view') // Category Page
    {
        $id = Mage::app()->getRequest()->getParam('id', false); //cat id
        $category = Mage::getModel('catalog/category')->load($id);
        $prodCol = $category->getProductCollection();
        $tool = $this->getLayout()->createBlock('catalog/product_list_toolbar')->setCollection($prodCol);
        $linkPrev = false;
        $linkNext = false;
        if ($tool->getCollection()->getSize()) {
            if ($tool->getLastPageNum() > 1) {
                if (!$tool->isFirstPage()) {
                    $linkPrev = true;
                    $prevUrl = $tool->getPreviousPageUrl();
                    if ($tool->getCollection->getCurPage() == 2) {
                        $prevUrl = str_replace('p=1&', '', $prevUrl);
                    }
                }
                if (!$tool->isLastPage()) {
                    $linkNext = true;
                    $nextUrl = $tool->getNextPageUrl();
                }
            }
        }
        ?>
        <?php if ($linkPrev): ?>
        <link rel="prev" href="<?php echo $prevUrl ?>" />
        <?php endif; ?>
        <?php if ($linkNext): ?>
        <link rel="next" href="<?php echo $nextUrl ?>" />
        <?php endif; ?>
<?php
    }
?>

Obviously, the better way would be better to use Magento to parse that url, and unset the pagenumber query parameter (pulling in turn the query parameter 'p' from the appropriate place).

964

Answer

Solution:

In my case I want to incorporate the prev and next links inside the defaut paging so I replaced $this->getPagerHtml() by this :

<div >
  <strong><?php
  $url=  ($this->getCurrentPage()==1)?
  $this->getPagerUrl(array($this->getOrderVarName()=>$order, $this->getDirectionVarName()=>$direction,$this->getPageVarName()=>null )).'?':
  $this->getPagerUrl(array($this->getOrderVarName()=>$order, $this->getDirectionVarName()=>$direction,$this->getPageVarName()=>null )).'&';
   echo $this->__('Page:') ?></strong>
  <ol>
  <?php if (!$this->isFirstPage()): ?>
    <li>
      <a 
      href="<?php echo $url.'p='.($this->getCurrentPage()-1);?>" title="<?php echo $this->__('Previous') ?>">
        <?php if(!$this->getAnchorTextForPrevious()): ?>
          <img src="<?php echo $this->getSkinUrl('images/pager_arrow_left.png') ?>" alt="<?php echo $this->__('Previous') ?>" />
        <?php else: ?>
          <?php echo $this->getAnchorTextForPrevious() ?>
        <?php endif;?>
      </a>
    </li>
  <?php endif;?>
  <?php
  $pages_Num= ($this->getTotalNum()%$this->getLimit())?(($this->getTotalNum()/$this->getLimit())+1):$this->getTotalNum()/$this->getLimit();
  for($_page=1;$_page<=$pages_Num;$_page++){
    if($this->getCurrentPage()==$_page) echo '<li ><a href="'.$url.'p='.$_page.'">'.$_page.'</a></li>';
    else  echo '<li><a href="'.$url.'p='.$_page.'">'.$_page.'</a></li>';
  }
  ?>
  <?php if (($this->getCurrentPage()+1)<$pages_Num): ?>
    <li><a 
    href="<?php echo $url.'p='.($this->getCurrentPage()+1);?>" title="<?php echo $this->__('Next') ?>">
        <?php if(!$this->getAnchorTextForNext()): ?>
          <img src="<?php echo $this->getSkinUrl('images/pager_arrow_right.png') ?>" alt="<?php echo $this->__('Next') ?>" />
        <?php else: ?>
          <?php echo $this->getAnchorTextForNext() ?>
        <?php endif;?>
      </a></li>
  <?php endif;?>
  </ol>

</div>

People are also looking for solutions to the problem: php - Assistance with CodeIgniter login script

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.