rel=“下一个”和 rel=“上一个”用于magento分页

发布于 2024-12-23 08:37:27 字数 2151 浏览 4 评论 0原文

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

我正在使用 Magento: Put “产品列表分页器”上发布的 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" />

像正常的类别 url,这是正常的首页。否则谷歌会感到困惑。任何人都可以帮我将 rel="prev" 标签从第二页更改为第一页。其他一切都工作正常。预先非常感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

a√萤火虫的光℡ 2024-12-30 08:37:27

我会考虑在设置前一个 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
    }
?>

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
    }
?>
路弥 2024-12-30 08:37:27

刚刚在 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');

对我来说似乎更干净。

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.

躲猫猫 2024-12-30 08:37:27

最简单的方法就是通过简单的字符串替换将其从字符串中删除:

    <?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 查询参数(依次从合适的地方)。

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).

一紙繁鸢 2024-12-30 08:37:27

就我而言,我想将上一个和下一个链接合并到默认分页中,因此我将 $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>

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

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文