以洋红色显示所有儿童类别及其子女

时间:2011-08-05 17:43:45

标签: magento categories

所以我希望这段代码在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;?>

3 个答案:

答案 0 :(得分:7)

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

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

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

答案 1 :(得分:3)

尝试将这些父类别的“Is Anchor”设置更改为“是”。这应该允许他们在没有任何自定义编码的情况下显示他们的子类别的产品。

答案 2 :(得分:0)

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

为了未来访问者的利益,上面的答案之一谈到将父类别的Is Anchor更改为是,以便向孩子们展示。 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>

希望这有助于某人。

相关问题