问题
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