1

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

我正在使用Magento 上发布的 shadowice222 中的代码: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
    }
?>

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

<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

4 回答 4

0

在我的情况下,我想将 prev 和 next 链接合并到默认分页中,因此我将 $this->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>
于 2013-07-18T17:19:03.693 回答
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”)。

于 2012-09-24T09:52:45.230 回答
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
    }
?>
于 2011-12-29T03:51:55.723 回答
0

只需阅读 Inchoo.net 上的新博客 -> 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');

对我来说似乎更干净一些。

于 2012-04-13T10:55:55.587 回答