对于magento分页,rel =“next”和rel =“prev”

时间:2011-12-28 21:08:20

标签: php magento pagination next rel

您好我想将类别分页的rel =“next”和rel =“prev”标签放入我的magento网站的部分。对于rel =“next”和rel =“prev”标记的detials,请参阅http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html

我正在使用Magento: Put "product list pager"-block in <head>上发布的shadowice222的代码。

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

我遇到了问题,在我的第二个分页页面上是

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

这应该是

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

与普通类别网址一样,这是正常的第一页。否则谷歌会感到困惑。任何人都可以帮我解决从第二页到第一页的rel =“prev”标签。其他一切都很好。非常感谢你提前。

4 个答案:

答案 0 :(得分:0)

我会考虑在设置前一个url链接的代码中添加一些内容:

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

答案 1 :(得分:0)

刚刚阅读Inchoo.net上的新博客 - &gt; http://inchoo.net/ecommerce/magento/how-to-implement-relprev-and-relnext-to-magentos-pagination/

他们稍微调整了代码

而不是

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

他们使用

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

对我来说似乎有点干净。

答案 2 :(得分:0)

最简单的方法是使用简单的字符串替换将其从字符串中删除:

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

显然,更好的方法是使用Magento解析该url,并取消设置pagenumber查询参数(从适当的位置依次提取查询参数'p')。

答案 3 :(得分:0)

在我的情况下,我想在defaut分页中加入prev和next链接,所以我用这个替换了$ this-&gt; getPagerHtml():

<div class="pages">
    <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 class="previous<?php if(!$this->getAnchorTextForPrevious()): ?> i-previous<?php endif;?>" 
            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') ?>" class="v-middle" />
                <?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 class="current"><a class="current" 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 class="next<?php if(!$this->getAnchorTextForNext()): ?> i-next<?php endif; ?>" 
        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') ?>" class="v-middle" />
                <?php else: ?>
                    <?php echo $this->getAnchorTextForNext() ?>
                <?php endif;?>
            </a></li>
    <?php endif;?>
    </ol>

</div>