Magento - Display only 'root category' in top menu

Magento - Display only 'root category' in top menu

July 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Display only 'root category' in top menu

Displaying a Root Category with Subcategories in Magento’s Top Menu

As an experienced developer working frequently with e-commerce platforms like Magento, I understand how crucial it is to have a clean and organized top menu that effectively guides users through your online store. In this post, we’ll walk you through the process of displaying only the root category as a main menu item, with all its subcategories and sub-subcategories appearing in a dropdown underneath.

The Challenge

The default Magento setup includes a top menu that displays every single category on your site. While this might seem like a good idea initially, it often leads to an overly crowded and confusing interface for both customers and staff members. By focusing only on the root categories and their respective subcategories, you can create a more intuitive navigation experience.

The Current Setup

Let’s start by examining the code snippet provided in the question:

<?php foreach ($this->getStoreCategories() as $_category): ?>
    <?php echo $this->drawItem($_category) ?>
<?php endforeach ?>

This code is located within the top.phtml file of your Magento theme and is responsible for rendering all categories across the store. It uses a simple foreach loop to iterate through every category returned by the getStoreCategories() method, drawing each one using the drawItem() function.

The Goal: Display Only Root Categories

Our objective is to modify this code so that it only displays the root categories as main menu items, with their respective subcategories appearing in a dropdown format. To achieve this, we need to identify and filter out non-root categories before rendering them.

The Solution: Filtering Non-Root Categories

To filter non-root categories, we can use Magento’s category hierarchy and level attribute. Here’s how you can modify the existing code:

<?php foreach ($this->getStoreCategories() as $_category): ?>
    <?php if ($_category->getLevel() == 0): ?>
        <?php echo $this->drawItem($_category) ?>
        <div class="dropdown-menu">
            <?php foreach ($_category->getChildrenCategories() as $_childCategory): ?>
                <?php echo $this->drawItem($_childCategory, array('class' => 'subcategories')) ?>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
<?php endforeach ?>

In this updated code:

  1. We’ve added a conditional statement to check if the current category has a level of 0 ($_category->getLevel() == 0). This condition ensures that only root-level categories are processed.

  2. If the category is indeed a root category, we first draw it as usual using drawItem().

  3. We then create a new div element with the class “dropdown-menu” to hold all the subcategories for this particular root category.

  4. Inside this dropdown menu div, we iterate through each child category of the current root category ($_category->getChildrenCategories()) and draw them using the same drawItem() function. To distinguish these subcategories from other top-level categories, we pass an array with a ‘class’ option set to “subcategories” as the second argument of drawItem().

  5. The rest of the code remains unchanged, ensuring that non-root category rendering is skipped.

Tweaking the Menu Appearance

While this modification significantly improves your menu’s structure and readability, you might want to further customize its appearance to match your theme’s design guidelines. You can do this by adding custom CSS classes or IDs to the root categories and their respective dropdown menus. For example:

<div class="dropdown-menu <?php echo $_category->getId() ?>">
    ...
</div>

Conclusion

By following these steps, you’ll successfully configure your Magento store’s top menu to display only root category items with their corresponding subcategories appearing in a neat dropdown format. This change not only enhances the user experience but also simplifies navigation for both customers and admin staff.

Remember, customizing your e-commerce platform’s layout is an integral part of creating a unique brand identity online. At Laravel Company, we specialize in providing comprehensive Magento development services, including menu customization, theme optimization, and extension development to help businesses like yours stand out from the competition.

If you find yourself needing professional assistance with your Magento store’s navigation or any other development requirements, don’t hesitate to reach out to our team of experienced developers. We’re always ready to help you create a seamless and engaging online shopping experience for your valued customers.

Happy coding!

Catalog Themes Customers

Our network