2013-01-11 72 views
11

如何获得所有类别和子类别,如果该类别处于活动状态,但“在导航菜单中包含”设置为“否”?我如何获得所有类别和子类别?

我尝试使用此:

<?php 
$_categories = Mage::getBlockSingleton('catalog/navigation'); 
foreach ($_categories->getStoreCategories() as $_category) { 
$category = Mage::getModel('catalog/category'); 
$category->load($_category->getId()); 
$subcategories = explode(',', $category->getChildren()); 
?> 
<dl> 
<dt><?php echo $this->htmlEscape($_category->getName()); ?></dt> 
<dd> 
<ol> 
<?php 
foreach ($subcategories as $subcategoryId) { 
$category->load($subcategoryId); 
echo '<li><a href="' . $category->getURL() . '">' . $category->getName() . '</a></li>'; 
} 
?> 
</ol> 
</dd> 
</dl> 
<?php 

} 
?> 

但是,如果一个类的“在导航菜单中包含‘是’否”,它不会显示在头版!

回答

29

你只需要改变一件事!当您致电$_categories = Mage::getBlockSingleton('catalog/navigation')时,您实际上是从catalog/navigation模型中特别抓取类别 - “非导航”类别的过滤已经完成。相反,我们可以从catalog/category型号抓住一个集合,以确保我们得到了所有类别在网站上提供:

$categories = Mage::getModel('catalog/category') 
     ->getCollection() 
     ->addAttributeToSelect('*') 
     ->addIsActiveFilter(); 

请注意,我用的addIsActiveFilter(),以确保我们只得到一个当前激活/启用类别。

+1

谢谢,你真是帮了!显示什么是必要的,然后我将它应该是它! – Oleg

3

我更喜欢使用目录/类别帮手

$helper = Mage::helper('catalog/category'); 
$categories = $helper->getStoreCategories(); 
相关问题