Show all subcategories of current parent caregory in sidebar navigation in MAGENTO

Show all subcategories of current parent caregory in sidebar navigation in MAGENTO

July 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Show all subcategories of current parent caregory in sidebar navigation in MAGENTO

Building Multi-Level Category Navigation Sidebar in Magento

In Magento, building a nested sidebar navigation for your product categories can significantly enhance user experience by providing an organized path to various products. In this post, we’ll walk through creating a multi-level category tree that expands and contracts based on the active parent category.

Understanding the Current Issue

The current implementation has two main limitations:

  1. The menu collapses entirely once you reach the deepest subcategory level.
  2. It doesn’t create the desired nested structure for the sidebar navigation.

Let’s address these issues step by step.

Creating a Recursive Function for Category Hierarchy

To generate this nested hierarchy, we need to create a recursive function that can traverse down through all child categories. This function will be called initially with the root category and then recursively with each subcategory until it reaches the leaf nodes.

Here’s an example of how you might define such a function:

public function getCategoryChildren($category)
{
    if (count($category->getChildrenCategories())) {
        foreach ($category->getChildrenCategories() as $child) {
            echo '<li class="nested"><a href="' . $this->getCategoryUrl($child) . '">' . $child->getName() . '</a>';

            // Recursively call the function with each child category
            $this->getCategoryChildren($child);

            echo '</li>';
        }
    }
}

Integrating the Recursive Function into Your View

Now, let’s update your sidebar.phtml file to use this recursive function. We’ll remove the current foreach loop and replace it with a single call to our new function:

<aside id="sidebar">
  <div class="sidebar-nav">
    <h2><?php echo $this->__('Product Categories') ?></h2>
    <ul>
      <?php $this->getCategoryChildren($current_cat) ?>
    </ul>
  </div>
</aside>

Improving the Menu Logic

To ensure that only the relevant submenus are visible at any given time, we need to update some core Magento files. Specifically, we’ll modify app/code/core/Mage/Catalog/Block/Navigation.php and add a conditional check for whether the current category has children:

// Inside the getHtml() function

if ($category->getChildren()) {
    $html .= '<ul class="level1" style="' . (count($category->getChildren()) ? 'display: none;' : '') . '">';
} else {
    $html .= '</ul>';
}

foreach ($category->getChildren() as $_child) {
    if ($_child instanceof Mage_Catalog_Model_Category && $_child->getIsActive()) {
        // Rest of the code...
    }
}

This modification ensures that submenus are initially hidden and only displayed when their parent category is selected.

Implementing the Expanded/Collapsed Behavior

To make the menu expandable/collapsible, we can use some simple JavaScript and CSS. The JavaScript will toggle the display property of the nested <ul> elements based on whether their parent <li> has been clicked:

$('li.nested').click(function() {
    $(this).find('> ul').toggle();
});

In your CSS, you can style these collapsed/expanded states using a simple rule like this:

#sidebar ul li ul {
    display: none;
}

This will ensure that all submenus are initially hidden.

Conclusion

Building a multi-level category navigation sidebar in Magento requires a combination of PHP logic to traverse the category tree and JavaScript/CSS to manage the visibility states. The key is to create a recursive function for traversing the hierarchy, then integrate this function into your view template while properly handling the expanded/collapsed behavior.

Remember that modifying core files like Mage_Catalog_Model_Category should be done with caution, as these changes will be overwritten in future Magento updates. Consider using a module or extension to make these modifications instead.

For more advanced customizations and development best practices in Magento, I recommend checking out the resources available at LaravelCompany.

Happy coding!

Catalog Errors

Our network