0

Here is the code which displays all the categories and sub categories

<?php echo $this->renderCategoriesMenuHtml(TRUE, 0, 'level-top'); ?>

I would like to display only subcategories of category id 10.

  • where did u add above code???? – rashmi sm Oct 8 '15 at 4:53
2

IN the below code you pass the category ID's like 4 and got all the subcategories of category.

   <?php
    $cat = Mage::getModel('catalog/category')->load(4);
    $subcats = $cat->getChildren();

    foreach(explode(',',$subcats) as $subCatid)
    {
      $_category = Mage::getModel('catalog/category')->load($subCatid);
      if($_category->getIsActive()) {
        echo '<ul><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a>';
        $sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
        $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>';
                  $sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
                  $sub_sub_subcats = $sub_sub_cat->getChildren();
                  foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
                  {
                    $_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
                    if($_sub_sub_category->getIsActive()) {
                        echo '<li class="sub_cat"><a href="'.$_sub_sub_category->getURL().'" title="View the products for the "'.$_sub_sub_category->getName().'" category">'.$_sub_sub_category->getName().'</a></li>';
                    }
                  }
               }
         }
         echo '</ul>';
      }
    }

    ?>


        ?>
  • I've been trying to only show subcategories of a parent category on the side bar for a moment now. My case is, if I were to click on, let's say "MEN" on the megamenu, I want the sidebar to display only the sub-categories of "MEN" within the sidebar. Any ideas on this? Any help would be much appreciated, thanks heaps. – borislemke Aug 20 '15 at 7:41
0
<?php
    $categoryId = 10 ;  // get current category id
    $category = Mage::getModel('catalog/category')->load($categoryId) ;
    $catList = explode(",", $category->getChildren();) ;
    foreach{$catList as $cat} 
    {
       $subcategory = Mage::getSingleton('catalog/category')->load($cat) ;
       echo $subcategory->getName() ; 
    }
?>

It will display the second level. If you want to display further levels, put this code in recursive.

  • Hi no sub categories displays here with this code,, any idea why? – Dips Jul 6 '14 at 2:58
  • 1
    pl try this code - in line 3, syntax error is there. $categoryId = 10 ; // get current category id $category = Mage::getModel('catalog/category')->load($categoryId) ; $catList = explode(",", $category->getChildren()) ; foreach ($catList as $cat) { $subcategory = Mage::getSingleton('catalog/category')->load($cat) ; echo $subcategory->getName()."<br>" ; } – alamelu Jul 7 '14 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.