Display Categories as top nav menu in magento

Display Categories as top nav menu in magento

July 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display Categories as top nav menu in magento

Displaying Magento Categories as a Top Navigation Menu

As a new Magento user, you may find it challenging to display your store’s categories in a clean, organized way within the header section of your website. This is especially true when dealing with subcategories that need to be revealed on hover.

I’ll guide you through the process of displaying your root category (‘Computer’) and its two subcategories (‘Mouse’ and ‘HDD’) as a top navigation menu where the root category appears in the main menu, and the subcategories appear as dropdown items upon mouseover.

Creating the Root Category

Firstly, ensure that your root category is created correctly:

  1. Log into your Magento Admin Panel.
  2. Navigate to Catalog > Categories.
  3. Click on “Add Root Category”.
  4. Fill in the required details (Name, Identifier) and click Save.

For your case, create a new category with:

  • Name: Computer
  • Identifier: computer

Adding Subcategories

Next, add subcategories under this root category:

  1. In the Magento Admin Panel, go again to Catalog > Categories.
  2. Find the ‘Computer’ category you just created and click on it.
  3. Click “Add Subcategory”.
  4. Fill in the details for your two subcategories:
    • Mouse: Name = Mouse, Identifier = mouse
    • HDD: Name = Hard Disk Drive (HDD), Identifier = hdd

Displaying Categories as Top Navigation Menu

To display these categories in the header section, you need to modify your theme’s header.phtml file. This file is typically located at:

app/design/frontend/[Your_Theme]/layout/default/template/page/html/header.phtml

Open this file and look for the following code block that handles the navigation menu:

<?php if(count($this->getStoreCategories())): ?>
    <div class="nav-container">
        <?php echo $this->drawMenuBar Categories::getActiveCategories() ?>
    </div>
<?php endif; ?>

This code draws the Magento menu bar using the active categories. You’ll notice that it uses the ::getActiveCategories() method, which retrieves all the current store’s active categories.

Modifying the Menu

To display your ‘Computer’ category and its subcategories as a top navigation menu:

  1. Open your theme’s header.phtml file.
  2. Replace the code block with this modified version:
<?php if(count($this->getStoreCategories())): ?>
    <div class="nav-container">
        <?php $rootCategory = $this->getRootCategory() ?>
        <ul class="top-nav">
            <li><a href="<?php echo $this->getUrl('catalogcategory/view', array('id' => $rootCategory->getId(), 'path' => 'computer')) ?>"><?php echo $rootCategory->getName(); ?></a>
                <?php if(count($rootCategory->getAllChildren())): ?>
                    <ul class="sub-nav">
                        <?php foreach ($rootCategory->getAllChildren() as $_child): ?>
                            <li><a href="<?php echo $this->getUrl('catalogcategory/view', array('id' => $_child->getId(), 'path' => 'computer')) ?>"><?php echo $_child->getName(); ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        </ul>
    </div>
<?php endif; ?>

Here, we retrieve the root category using $this->getRootCategory(), and then loop through its subcategories. The getUrl() function is used to generate the URLs for each category.

Conclusion

Displaying Magento categories as a top navigation menu requires a bit of manual modification in your theme’s header.phtml file. By following this guide, you should now be able to display your ‘Computer’ root category and its subcategories (‘Mouse’ and ‘HDD’) in the header section exactly as described.

Remember, Magento is highly customizable, so always refer to the official Magento documentation for more detailed instructions on theme modifications: https://laravelcompany.com/magento-theme-development.

Happy coding!

Catalog Themes Admin SEO

Our network