0

次のコードを使用して、現在所有しているカテゴリのサブカテゴリを表示しています。

ただし、2つのレベルのサブ猫がいるので、コードを変更して、両方のレベルではなく、現在所有している現在のカテゴリのサブカテゴリの最初のレベルのみを表示できるようにする必要がありますか?

<?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>';
            }
        }      
    }
}
?> ` 
4

2 に答える 2

0

If you just want to show the first level subcategories, then just get rid of

/*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>';
    }
}  

Since you're now loading the current category through $cat = Mage::registry('current_category'); and fetches is subcategories that you loop through (first level).

于 2012-09-14T21:19:12.013 に答える
0

You may use the $_sub_category->getLevel() to check the current level.

Try re-writing the below code

if($_sub_category->getIsActive()) {

to

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

Please make sure getLevel() returns the appropriate level (can't remember it is 1,2 or 3).

于 2012-09-14T21:19:17.243 に答える