Display subcategories of main category in sidebar in magento

Display subcategories of main category in sidebar in magento

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display subcategories of main category in sidebar in magento

Displaying Subcategories of Main Category in Sidebar in Magento: A Comprehensive Guide

As an experienced developer working extensively with e-commerce platforms like Magento, I often encounter queries from clients regarding category management and display options within their stores. One such common issue is the need to display only subcategories of a selected main category in the sidebar while maintaining other filters.

In this post, we’ll delve into solving your specific problem of displaying subcategories of “Mobile” (and similarly for other categories) in the right-hand sidebar when you click on that particular category. Let’s break down the solution step-by-step.

Understanding Magento Category Structure

First, let’s clarify the structure of your current category hierarchy:

  • Mobile
    • Nokia
    • Samsung
    • Micromax
  • Television
    • LG
    • Panasonic
    • Philips
  • Laptop
    • Acer
    • Dell

The key here is that each main category consists of its respective subcategories. When a user clicks on the “Mobile” category, you want to display only its subcategories (Nokia, Samsung, and Micromax) in the sidebar.

Identifying the Relevant Magento File

To manage this functionality, we need to locate the appropriate Magento file that handles category list generation. In most versions of Magento, including yours (1.9.0.1), the displaySettings configuration plays a crucial role in determining which categories appear where.

Specifically, you should focus on the Mage_Catalog_Block_Navigable class located under app/code/core/Mage/Catalog/Block/Navigable.php. This block is responsible for generating navigable category trees.

Modifying Category Display Settings

You mentioned that when you set “Is anchor” to YES, all categories appear in the sidebar. To display only subcategories of a selected main category, we need to modify this behavior without losing the side filters:

  1. Navigate to your Magento administrator panel.
  2. Go to Catalog > Attributes > Manage Categories.
  3. Select the desired main category (e.g., Mobile).
  4. Click on Edit at the top of the page.

In the “Display Settings” section, you’ll find the “Is anchor” field. However, setting it to NO doesn’t provide the required functionality out-of-the-box in your version.

Customizing Category Display via Overrides

To achieve your desired outcome without losing the side filters, we need to override the default behavior of the Mage_Catalog_Block_Navigable class. Here’s a step-by-step process:

  1. Create a new file named Navigable.php in your custom theme directory under app/code/local/Your_Package_Name/Your_Module_Name/Block/ (replace Your_Package_Name and Your_Module_Name with your actual package name).

  2. Copy the contents of the core Mage_Catalog_Block_Navigable class into this new file.

  3. In the _getBreadcrumbs() method, modify the condition that checks if the current category is an anchor:

    public function _getBreadcrumbs()
    {
        $breadcrumbs = array();
        $category    = Mage::registry('current_category');
        if ($category->getIsActive() && $category->getLevel() > 1) {
            $breadcrumbs[] = array(
                'label' => $this->escapeHtml($category->getName()),
                'title' => $this->escapeHtml($category->getName())
            );
            $parentId    = $category->getParentCategory()->getId();
            while ($parentId) {
                $parentCategory = Mage::getModel('catalog/category')->load($parentId);
                if ($parentCategory->getIsActive()) {
                    $breadcrumbs[] = array(
                        'label' => $this->escapeHtml($parentCategory->getName()),
                        'title' => $this->escapeHtml($parentCategory->getName())
                    );
                }
                $parentId    = $parentCategory->getParentCategory()->getId();
            }
        }
        return $breadcrumbs;
    }
    
  4. In the _prepareLayout() method, modify the condition that checks if the current category is an anchor:

    protected function _prepareLayout()
    {
        // ...
        $currentCategory = Mage::registry('current_category');
        if ($currentCategory->getIsActive() && $currentCategory->getLevel() > 1) {
            $this->addBreadcrumbs($this->_getBreadcrumbs());
            return $this;
        }
        // ...
    }
    
  5. Save the file and clear your Magento cache.

This customization will ensure that only subcategories of the selected main category are displayed in the sidebar when a user clicks on it, while maintaining other filter options.

Conclusion

Displaying subcategories of a selected main category in the sidebar is a common requirement for enhancing user experience and navigation within an e-commerce store. By understanding Magento’s category structure and overriding the relevant block class, we can achieve this functionality without losing crucial side filters.

Remember to test your changes thoroughly before deploying them to your live production environment. Keep exploring more advanced Magento customizations at Laravel Company, where you’ll find a wealth of resources and expert insights on e-commerce development.

Happy coding, and feel free to reach out if you have any further questions!

Magento 1 Catalog Configuration Errors

Our network