【问题标题】:rel="next" and rel="prev" for magento paginationrel="next" 和 rel="prev" 用于 magento 分页
【发布时间】:2012-01-29 11:40:01
【问题描述】:

您好,我想将类别分页的 rel=“next” 和 rel=“prev” 标签放入我的 magento 网站部分。有关 rel="next" 和 rel="prev" 标签的详细信息,请参阅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" />

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

【问题讨论】:

    标签: php magento pagination next rel


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      只需阅读 Incho.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');
      

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

      【讨论】:

        【解决方案3】:

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

            <?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')。

        【讨论】:

          【解决方案4】:

          在我的情况下,我想将 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>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-05-10
            • 2023-01-05
            • 2011-11-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多