【问题标题】:How I can get all categories and subcategories?如何获取所有类别和子类别?
【发布时间】:2012-12-26 03:14:18
【问题描述】:

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

我尝试使用这个:

<?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

} 
?> 

但如果某个类别的“包含在导航菜单中”为“否”,则它不会显示在首页上!

【问题讨论】:

    标签: magento navigation categories


    【解决方案1】:

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

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

    请注意,我使用addIsActiveFilter() 来确保我们只获取当前处于活动/启用状态的类别。

    【讨论】:

    • 谢谢,你真的帮了大忙!显示什么是必要的,然后我会按原样带来!
    【解决方案2】:

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

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

    【讨论】:

    • 我什么都没得到,$helper = Mage::helper('catalog/category'); $categories = $helper->getStoreCategories(); foreach ($categories as $_category) { print_r($_category->getName());死(); }
    猜你喜欢
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2023-03-17
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多