【问题标题】:Showing only the first level of sub categories for the current category仅显示当前类别的第一级子类别
【发布时间】:2015-10-26 00:31:49
【问题描述】:

我正在使用以下代码来显示您拥有的当前类别的子类别。

但是我有 2 个级别的子猫,所以我需要能够以某种方式修改代码以仅显示您拥有的当前类别的第 1 级子类别,而不是两个级别?

<?php 
/* Load category by id */
$cat = Mage::registry('current_category');

 /*Returns comma separated ids*/
$subcats = $cat->getChildren();

foreach(explode(',',$subcats) as $subCatid)
{
    $_category = Mage::getModel('catalog/category')->load($subCatid);
    if($_category->getIsActive()) {
        echo '<li><a href="'.$_category->getURL().'" title="View the products for the  "'.$_category->getName().'" category">'.$_category->getName().'</a></li>';

        /* Load category by id */
        $sub_cat = Mage::getModel('catalog/category')->load($_category->getId());

        /*Returns comma separated ids*/
        $sub_subcats = $sub_cat->getChildren();
        foreach(explode(',',$sub_subcats) as $sub_subCatid)
        {
            $_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
            if($_sub_category->getIsActive()) {
                echo '<li class="sub_cat"><a href="'.$_sub_category->getURL().'" title="View the products for the "'.$_sub_category->getName().'" category">'.$_sub_category->getName().'</a></li>';
            }
        }      
    }
}
?> ` 

【问题讨论】:

    标签: php magento


    【解决方案1】:

    如果你只想显示第一级子类别,那就去掉

    /*Returns comma separated ids*/
    $sub_subcats = $sub_cat->getChildren();
    foreach(explode(',',$sub_subcats) as $sub_subCatid)
    {
        $_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
        if($_sub_category->getIsActive()) {
            echo '<li class="sub_cat"><a href="'.$_sub_category->getURL().'" title="View the products for the "'.$_sub_category->getName().'" category">'.$_sub_category->getName().'</a></li>';
        }
    }  
    

    由于您现在通过 $cat = Mage::registry('current_category'); 加载当前类别,并且 fetches 是您循环遍历的子类别(第一级)。

    【讨论】:

      【解决方案2】:

      您可以使用$_sub_category-&gt;getLevel()查看当前级别。

      尝试重写下面的代码

      if($_sub_category->getIsActive()) {
      

      if($_sub_category->getIsActive() && $_sub_category->getLevel() == 2) {
      

      请确保 getLevel() 返回适当的级别(不记得是 1,2 还是 3)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        • 1970-01-01
        • 2018-04-16
        • 2020-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多