如何在magento的子类别集合中添加分页?

时间:2014-06-26 07:07:18

标签: php magento pagination

我编写了以下代码来显示子类别

    <ul class="">
  <?php
$_categories=$this->getCurrentChildCategories();
if($_categories->count()):
$categorycount = 0;
foreach ($_categories as $_category):
if($_category->getIsActive()):
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$catName = $this->getCurrentCategory()->getName();
?>
  <li class="">
    <div class="">
      <div class="">
        <div class=""> <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="">
          <?php
                    $imageUrl = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/".$this->getCurrentCategory()->getThumbnail();

                    $imageResized = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/"."resize/".$this->getCurrentCategory()->getThumbnail();


                    if (!file_exists($imageResized) && file_exists($imageUrl)) 
                    {

                    $imageObj = new Varien_Image($imageUrl);
                    $imageObj->constrainOnly(TRUE);
                    $imageObj->keepAspectRatio(TRUE);
                    $imageObj->keepFrame(FALSE);
                    $imageObj->quality(100);
                    $imageObj->resize(270, 270);
                    $imageObj->save($imageResized);
                    }
                    ?>
          <span class=""><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/resize/'.$this->getCurrentCategory()->getThumbnail(); ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>"/></span> </a> </div>
      </div>
      <div class="">
        <div class="">
          <div class=""> <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a> </div>
        </div>
      </div>
    </div>
  </li>
  <?php
endif;

endforeach;
endif;
?>
</ul>

我想在此页面中添加分页,我尝试了以下解决方案,但它不起作用

parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
    $pager->setAvailableLimit(array(15=>15));
    $pager->setCollection($_categories);

    $this->setChild('pager', $pager);

有人可以知道如何在子类别列表页面中添加分页吗?

请指导我,因为我是magento开发的新手。

提前致谢。

1 个答案:

答案 0 :(得分:0)

在Mage_Catalog_Block_Navigation里面我有自定义方法:

public function getCurrentChildCategories()
{
    if (null === $this->_currentChildCategories) {
        $layer = Mage::getSingleton('catalog/layer');
        $category   = $layer->getCurrentCategory();
        $this->_currentChildCategories = Mage::getModel('catalog/category')->getCollection();

        $pager = new Mage_Page_Block_Html_Pager();
        $pager->setLimit(100)->setCollection($this->_currentChildCategories);
        $this->setChild('pager', $pager);

        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        $this->_currentChildCategories->addAttributeToSelect('url_key')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_anchor')
            ->addAttributeToSelect('image') 
            ->addAttributeToFilter('is_active', 1)
            ->addIdFilter($category->getChildren())
            ->setOrder('name', 'ASC')
            ->joinUrlRewrite()
            ->load();

        $productCollection = Mage::getResourceModel('catalog/product_collection');
        $layer->prepareProductCollection($productCollection);
        $productCollection->addCountToCategories($this->_currentChildCategories);
    }
    return $this->_currentChildCategories;
}

我认为这不是最好的,但它有效。