How to display the categories and subcategories in a tree format?

How to display the categories and subcategories in a tree format?

October 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to display the categories and subcategories in a tree format?

Displaying categories and subcategories in a tree format is a common requirement for e-commerce websites to provide a structured navigation experience for users. In Magento, you can achieve this using the built-in category structure along with some custom PHP code.

Understanding the Category Structure

Magento organizes products into a hierarchical category structure. The top-level categories are called root categories or parent categories. These categories can contain subcategories (child categories). This nested structure allows for a well-organized product catalog.

Displaying Categories and Subcategories

To display the categories in a tree format, you’ll need to iterate through the categories and their child categories using PHP loops. Here’s an example of how you can achieve this:

<?php $_helper = Mage::helper('catalog/category'); ?>
<?php $_categories = $_helper->getStoreCategories(); ?>

<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach ($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category); ?>">
                    <?php echo $_category->getName(); ?>
                </a>

                <?php
                $_category = Mage::getModel('catalog/category')->load($_category->getId());
                $_subcategories = $_category->getChildrenCategories();
                if (count($_subcategories) > 0):
                ?>
                    <ul>
                        <?php foreach ($_subcategories as $_subcategory): ?>
                            <li>
                                <?php echo '&nbsp;&nbsp;&nbsp;-'; ?>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory); ?>">
                                    <?php echo $_subcategory->getName(); ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

This code retrieves all the store categories using Mage::helper('catalog/category')->getStoreCategories(). It then iterates through each category and checks if it has any child categories. If a category does have subcategories, it recursively displays them in an unordered list (<ul>).

Displaying Products for Each Subcategory

To display the products of a specific subcategory when clicked, you can use Magento’s built-in category page layout. When a user clicks on a subcategory link, they are redirected to the category page associated with that subcategory.

By default, Magento displays all the products belonging to the selected category in a grid format on the category page. You don’t need any additional PHP code for this; it is handled by the Magento core functionality.

Styling the Tree Format

To make the tree format visually appealing, you can use CSS to style the unordered lists (<ul>) and list items (<li>). You can also add some interactivity using JavaScript or jQuery to collapse/expand subcategories when clicked.

Conclusion

Displaying categories and subcategories in a tree format is an essential part of providing a user-friendly navigation experience on your Magento e-commerce website. By utilizing the built-in category structure and PHP loops, you can achieve this functionality without having to write complex custom code.

Remember to test your implementation thoroughly across different devices and browsers to ensure consistent performance. If you encounter any issues or need further assistance with Magento development, consider reaching out to experts at Laravel Company.

Catalog

Our network