【问题标题】:Display all children categories and their children in magento在 magento 中显示所有子类别及其子类别
【发布时间】:2011-08-05 17:43:45
【问题描述】:

所以我希望这段代码在 app/frontend/default/default/catalog/category/ 中的 view.phtml 页面上显示类别子项及其子项

因此,当您看到类别页面时,您会看到所有子类别中的所有孩子

这是我得到的,它显示子类别,但不显示它们的子类别。

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

   <ul>
    <?foreach ($collection as $cat):?>
            <?php if($_category->getIsActive()):?>
            <?php 
                 $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
                 $_img = $cur_category->getImageUrl();  
            ?>
            <li>
                            <a href="<?php echo $helper->getCategoryUrl($cat);?>">
                                 <img src="<?php echo $_img?>" title="$cat->getName()"/>
                                 <cite><?php echo $cat->getName();?></cite>
                            </a>
                    </li>
            <?php endif?>

<?php endforeach;?>

【问题讨论】:

  • 管理中的类别是否设置为“锚点”?这通常会使他们自动包含他们的孙辈。

标签: magento categories


【解决方案1】:

请尝试使用以下代码。 主要思想是获取主要级别类别。为每个类别获取子类别。

<?php
      /* Get the categories that are active for the store */
      $_main_categories=$this->getStoreCategories();

      /* Get the current category the user is in */
      $_current_category=$this->getCurrentCategory();

      /* Get the current category path */
      $_categorypath = $this->getCurrentCategoryPath();
?>

    <?php if ($_main_categories): ?>
        <div class="box normal-nav">
            <div class="box-top">
            </div>
            <div class="box-content">
                    <ul>
                        <?php
                            /* This bit cycles through the categories - setting the next one to current */
                            foreach ($_main_categories as $_main_category):
                                if($_main_category->getIsActive()):
                                    $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId());
                                    $layer = Mage::getSingleton('catalog/layer');
                                    $layer->setCurrentCategory($cur_category);
                        ?>

                                    <li><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a>

                                        <?php $_maincategorylisting=$this->getCurrentCategory()?>

                                        <?php $_categories=$this->getCurrentChildCategories()?>

                                        <?php if($_categories->count()): ?>
                                            <ul class="subcategory">
                                                <? foreach ($_categories as $_category):?>
                                                   <? if($_category->getIsActive()):
                                                          $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId());
                                                          $layer = Mage::getSingleton('catalog/layer');
                                                          $layer->setCurrentCategory($cur_subcategory);
                                                   ?>

                                                          <li><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></li>
                                                   <? endif;?>

                                                 <?endforeach?>

                                            </ul>
                                            <?php $layer->setCurrentCategory($_current_category);  ?>

                                        <? endif; ?>
                                    </li>

                             <?php endif; ?>

                        <?php endforeach; ?>
                    </ul>
                </div>
                <div class="box-bottom">

                </div>
        </div>
    <?php endif;  ?>

稍后添加:

如果为您的代码应用修复程序如下所示:

    <?php
        $_category  = $this->getCurrentCategory();
        $collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
        $helper     = Mage::helper('catalog/category');
    ?>
   <ul>
    <?php foreach ($collection as $cat):?>
            <li>
                <a href="<?php echo $helper->getCategoryUrl($cat);?>">
                    <cite><?php echo $cat->getName();?></cite>
                </a>
                <?php $childLevel2Category = Mage::getModel('catalog/category')->getCategories($cat->entity_id); ?>
                <ul>
                    <?php foreach ($childLevel2Category as $catLevel2) { ?>
                        <li>
                            <a href="<?php echo $helper->getCategoryUrl($catLevel2);?>">
                                <cite><?php echo $catLevel2->getName();?></cite>
                            </a>
                        </li>
                    <?php } ?>
                </ul>
            </li>
<?php endforeach;?>
   </ul>

如果您需要更多级别(更多子目录),请使用递归函数重写此构造。

【讨论】:

  • 此代码不起作用。没有显示类别,也没有显示它们的子项。
  • 请尝试底部提供的新解决方案。
【解决方案2】:

尝试将这些父类别的“是锚”设置更改为“是”。这应该允许他们展示其子类别的产品,而无需任何自定义编码。

【讨论】:

    【解决方案3】:

    我知道这是一个老问题,但我在尝试做同样的事情时在搜索中遇到了它。

    为了将来的访问者的利益,上面的答案之一是关于将父类别的 Is Anchor 更改为 Yes,以便向孩子展示。 OP说这对他不起作用。

    至关重要的是,完成此操作后,您需要重新索引您的类别/产品关联(系统->索引管理)。如果您有很多产品,这可能需要一些时间。

    但是这样做之后……瞧!后代产品已列出。

    并将来自 http://www.creare.co.uk/magento-subcategories-category-pages 的代码放在 template/catalog/category/list.phtml 中,以便显示子猫:

    <?php
    $category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
    $categories = $category->getCollection()
            ->addAttributeToSelect(array('name', 'thumbnail'))
            ->addAttributeToFilter('is_active', 1)
            ->addIdFilter($category->getChildren())
    ?>
    <ul class="subcategories">
        <?php foreach ($categories as $category): ?>
            <li>
                <a href="<?php echo $category->getUrl() ?>"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
                    <span><?php echo $category->getName() ?></span></a>
            </li>
        <?php endforeach; ?>
    </ul>
    

    希望这对某人有所帮助。

    【讨论】:

    • 这行得通,但是在控制器/助手类中包含代码的版本会更好。
    猜你喜欢
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多