Magento 如何获取所有类别和子类别?
如果类别处于活动状态,但“包含在导航菜单中”设置为“否”,如何获取所有类别和子类别 我试着用这个:Magento 如何获取所有类别和子类别?,magento,navigation,categories,Magento,Navigation,Categories,如果类别处于活动状态,但“包含在导航菜单中”设置为“否”,如何获取所有类别和子类别 我试着用这个: <?php $_categories = Mage::getBlockSingleton('catalog/navigation'); foreach ($_categories->getStoreCategories() as $_category) { $category = Mage::getModel('catalog/category'); $category->
<?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
}
?>
但是如果一个类别的“包含在导航菜单中”是“否”,它将不会显示在头版 你只需要改变一件事!当您调用
$\u categories=Mage::getBlockSingleton('catalog/navigation')
时,您实际上是从目录/导航
模型中获取类别,具体而言,从“非导航”类别中筛选出来的操作已经完成。相反,我们可以从目录/类别
模型中获取一个集合,以确保我们在网站上获得所有可用类别:
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter();
请注意,我使用的是
addIsActiveFilter()
,以确保只获取当前处于活动/启用状态的类别。我更喜欢使用catalog/category帮助程序
$helper = Mage::helper('catalog/category');
$categories = $helper->getStoreCategories();
谢谢,你真的帮了大忙!显示所需内容,然后我将按原样带来!我什么都没有,$helper=Mage::helper('catalog/category')$categories=$helper->getStoreCategories();foreach($categories as$_category){print_r($_category->getName());die();}