如何确定哪个顶级类别处于活动状态

时间:2013-05-13 12:10:24

标签: magento magento-1.7

在我的目录页面上,我想获得当前有效的顶级类别。

示例:

1. Top Level Category
  1.1 Sub Category
    1.1.1 Sub sub Category
    1.1.2 Sub sub Category
  1.2 Sub Category
2. Top Level Category
  2.1 Sub Category
  2.2 Sub Category

假设我在子分类1.1.1中。我怎么知道'1。顶级类别'是当前的顶级类别?

我尝试了以下内容,但它对我不起作用:

            <?php $_cat_helper = Mage::helper('catalog/category') ?>
            <?php $_categories = $_cat_helper->getStoreCategories() ?>

            <?php foreach ($_categories as $_category): ?>
                <?php if ($this->isCategoryActive($_category)): ?>
                <?php echo $_category->getName() ?> <br>
                <?php endif; ?>
            <?php endforeach; ?>

请帮助: - )


更新

如果我将$this->isCategoryActive($_category)放入catalog / product / list.phtml,我会收到以下错误:

Invalid method Mage_Catalog_Block_Product_List::isCategoryActive(Array
(
    [0] => 
)
)


Trace:
#0 .../default/template/catalog/product/list.phtml(53): Varien_Object->__call('isCategoryActiv...', Array)
#1 .../default/template/catalog/product/list.phtml(53): Mage_Catalog_Block_Product_List->isCategoryActive(NULL)

2 个答案:

答案 0 :(得分:1)

获取当前类别并爬上类别树。您想要级别2类别(级别1是目录根目录)。尝试这样的事情:

$category = Mage::getModel('catalog/category')
                    ->load(Mage::registry('current_category'));

while ($category->getLevel() != 2) {
    $category = Mage::getModel('catalog/category')
                        ->load($category->getParentId());
}

答案 1 :(得分:1)

想出来:

<?php $_categories = $this->getStoreCategories();?>

<?php foreach($_categories as $_category): ?>
      <?php if($this->isCategoryActive($_category)): ?>
            <?php echo $_category->getName(); ?>
      <?php endif; ?>
<?php endforeach; ?>

确保块类型为catalog/navigation