简体   繁体   中英

Magento: Put “product list pager”-block in <head>

I have the following problem: I want develop Pagination with rel=“next” and rel=“prev” for magento product lists. http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html How can I access the pager in ? The head-block has been already rendered when the pager-block will be rendered...

Is there any solution for my problem?

Yes, you can do edit the following file:

/app/design/frontend//default/template/page/html/pager.phtml

There you see for example:

<?php if (!$this->isFirstPage()): ?>
            <li>
                <a class="previous<?php if(!$this->getAnchorTextForPrevious()): ?> i-previous<?php endif;?>" href="<?php echo $this->getPreviousPageUrl() ?>" title="<?php echo $this->__('Previous') ?>">
                    <?php echo $this->__("Previous");?>
                </a>
            </li>
        <?php endif;?>

Where you can easily add the rel="..." you want to the link, same goes for the "Next" link ;)

I added this code into the head.phtml file...

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

From version 1.5 this solution doesn't work anymore, this is my solution:

    $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()->addAttributeToFilter('status', 1)->addAttributeToFilter('visibility', array('in' => array(2, 4)));
    $tool = $this->getLayout()->createBlock('page/html_pager')->setLimit($this->getLayout()->createBlock('catalog/product_list_toolbar')->getLimit())->setCollection($prodCol);
    $linkPrev = false;
    $linkNext = false;
    if ($tool->getCollection()->getSize()){
        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 .'" />';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM