I'm trying to show all the subcategories of the category id3. Category ID3 -> Category Level 2 for Category ID3 -> Category Level 3 (for category Level 2)

<?php if (!Mage::registry('current_category')) return ?>
$categoryId = 3;
$category = Mage::getModel('catalog/category')->load($categoryId);
$_categories = $category->getChildrenCategories();
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
<div class="block block-layered-nav block-layered-nav--no-filters">
    <div class="block-content toggle-content open">
        <dl id="narrow-by-list2">
            <dt><?php echo $this->__('Каталог') ?></dt>
                <?php foreach ($_categories as $_category): ?>
                    <?php if($_category->getIsActive()): ?>
                        <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>
                            <?php echo $this->escapeHtml($_category->getName()) ?>
                    <?php endif; ?>
                <?php endforeach ?>
        <script type="text/javascript">decorateDataList('narrow-by-list2')</script>
<?php endif; ?>

Try this

$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 hope this will help you.

  • hey! $cat->getChildren() will doesn't work if you have set 'use_flat_categories' ON, which is, in fact, absolutely recommended on production. Regardless that isn't a good practice at all, load models in phtml or any other view files, – MauroNigrele Dec 25 '15 at 20:31
  • Please help to combine this and my code. To keep the menu structure accordion. – Sylon Dec 27 '15 at 13:59

For some reason magento retrieve categories tree in different format depending on use category flat configuration, so take a look at code in:


    ... @line 246
    // get all children
    // If Flat Data enabled then use it but only on frontend
    $flatHelper = Mage::helper('catalog/category_flat');
    if ($flatHelper->isAvailable() && $flatHelper->isBuilt(true) && !Mage::app()->getStore()->isAdmin()) {
        $children = (array)$category->getChildrenNodes();
        $childrenCount = count($children);
    } else {
        $children = $category->getChildren();
        $childrenCount = $children->count();
    $hasChildren = ($children && $childrenCount);

As you can see, you could find how to do some staff in the right way just looking into the core code. If you can, try to grab the category model from some registry instead to load it again, on frontend you always have a category tree loaded ready to use.


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.