gpt4 book ai didi

magento - 在 magento 中显示所有子类别及其子类别

转载 作者:行者123 更新时间:2023-12-01 08:38:26 25 4
gpt4 key购买 nike

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

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

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

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

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

关于magento - 在 magento 中显示所有子类别及其子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6960444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com