1

我正在使用下面的代码来获取与当前页面处于同一级别的类别列表(通过列出该类别的父类别的子类别)。平面类别关闭时效果很好。启用平面类别后,H2 可以正常工作,但列表会更改为显示根的子类别,而不是此页面的父级子类别。我做错了什么,为什么会这样?

<?php
$_category  = $this->getCurrentCategory();
$parentCategoryId = $_category->getParentId();
$collection = Mage::getModel('catalog/category')->getCategories($parentCategoryId);
$helper     = Mage::helper('catalog/category');
$parentCategory   = Mage::getModel('catalog/category')->load($parentCategoryId);

?>
<h2><?php echo $parentCategory->getName(); ?></h2>

<ul>
<?php foreach ($collection as $cat):?>
        <?php if($_category->getIsActive()): ?>
            <li <?php
                if ($cat->getId() == $_category->getId()) {
                    echo 'class="current"';
                }
            ?>>
                <a href="<?php echo $helper->getCategoryUrl($cat);?>"><?php echo $cat->getName();?></a>
            </li>
        <?php endif; ?>
    <?php endforeach;?>
</ul>

是的,我已经刷新了缓存和索引。

4

1 回答 1

2

我不完全知道为什么 ->getCategories($id) 不是“安全的”,但使用 Stefan 的建议帮助我找到了替代方法。

我在顶部的新行如下所示:

$_category  = $this->getCurrentCategory();
$parentCategoryId = $_category->getParentId();
$helper     = Mage::helper('catalog/category');
$parentCategory   = Mage::getModel('catalog/category')->load($parentCategoryId);
$collection = $parentCategory->getChildrenCategories();

...无论打开或关闭“平面目录”,它都有效。

于 2013-09-06T15:12:08.257 回答