Magento categories listing using getCollection & addLevelFilter but exclude Default Root Category

旧时模样 提交于 2019-12-09 20:20:03

问题


I'm using the following code to grab a collection and to filter on a level using addLevelFilter(2). This outputs all my categories at level 2 perfectly except it also pulls through the Default Root Category in my list. I want to exclude this from view but having looked through all the available methods, I see nothing that will help me remove/exclude certain levels or categories. I really want a full proof solution so if I were to choose say level 3 it would only show level three and not level 1,2,3. Does anyone have a suggestion to pull only a certain level/categories!!??

<?php
$categories = Mage::getModel('catalog/category')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addIsActiveFilter()
                    ->addLevelFilter(2)
                    ->addOrderField('name');
    foreach($categories as $category):
?>
    <div class="home-cats">
        <div class="product-image">
           <a href="<?php echo $category->getURL() ?>" title="<?php echo $this->htmlEscape($category->getName()) ?>">
             <img src="<?php echo $category->getImageUrl() ?>" width="88" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
           </a>
        </div>
    </div>
<?php endforeach; ?>

Any advice/feedback would be greatly appreciated. Cheers, Sahus


回答1:


you can try

->addAttributeToFilter('level',2)

this may work for you



来源:https://stackoverflow.com/questions/9304240/magento-categories-listing-using-getcollection-addlevelfilter-but-exclude-defa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!