所以我希望这段代码在我的视图中显示类别的子类别及其子类别。php页面:app/frontend/default/default/catalog/category/
所以当你看到类别页面时你会看到所有子类别的所有子类别
这是我得到的,它显示子类别,但不显示其子类别。
<?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;?>
请尝试使用以下代码。主要思想是得到主要的层次范畴。对于每个类别,得到儿童类别。
<?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>
如果你需要更多的层次(更多的子目录)重写这个结构使用递归函数。
尝试将这些父类别的"Is Anchor"设置为"Yes"。这应该允许他们显示其子类别的产品,而无需任何自定义编码。
我知道这是一个老问题,但是我在尝试做同样的事情时在搜索中遇到了它。
为了将来访问者的利益,上面的一个答案谈到将父类别的锚改为Yes,以便显示子类别。医生说这对他没用。
至关重要的是,在这样做之后,您需要重新索引您的类别/产品关联(系统->索引管理)。如果你有很多产品,这可能需要一些时间。
但是这样做之后……瞧!后代产品列表
并将此代码从http://www.creare.co.uk/magento-subcategories-category-pages放在模板/目录/类别/列表中。php负责显示子猫:
<?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>