我使用此代码列出子类别
$root = Mage::getModel('catalog/category')->load(3); // Put your category ID here.
$subCat = explode(',',$root->getChildren());
$collection = $root->getCollection()->addAttributeToSelect("*")->addFieldToFilter("entity_id", array("in", $subCat) );
foreach($collection as $subcategory) {
echo '<a href="'.$subcategory->getURL() .'" />» '.$subcategory->getName().'</a><br/>';
}
我想只显示前3个子类别。我该怎么办?
答案 0 :(得分:5)
使用
$collection
// ->addFieldToFilter...
->setPageSize(20)
->setCurPage(1);
答案 1 :(得分:5)
尝试为集合添加限制 此外,您可以获得根目录ID而无需对其进行硬编码 您也不需要所有的根儿童ID。您可以按父ID过滤集合。
$rootId = Mage::app()->getStore()->getRootCategoryId();
$collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect("*")
->addFieldToFilter("parent_id", $rootId);
$collection->addAttributeToSort('position'); //sort by position
$collection->setPage(1, 3);//limit 3, page 1
foreach($collection as $subcategory) {
echo '<a href="'.$subcategory->getURL() .'" />» '.$subcategory->getName().'</a><br/>';
}
答案 2 :(得分:4)
使用以下
$collection= Mage::getModel('catalog/category')
->getCollection()
->setPageSize(3);
echo $collection->count();