Magento : From where the html of "<?php echo $_menu ?>" comes from?

Magento : From where the html of "<?php echo $_menu ?>" comes from?

August 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento : From where the html of "<?php echo $_menu ?>" comes from?

Where Does the HTML of "" Come From in Magento?

In the Magento framework, the page/html/topmenu.phtml file is responsible for generating the top menu items that you have created in your store’s backend. By default, this file uses a simple PHP echo statement to output the menu:

<ul id="nav">
    <?php echo $_menu ?>
</ul>

The variable $_menu contains the HTML markup for each menu item as a string. This is generated by the Magento core using the Mage_Page_Block_Topmenu::getHtml() method.

Editing the Menu Item Classes

To edit the <li> tags and their classes produced by $_menu, you’ll need to modify the core file itself or extend it through your theme’s layout XML file. Here’s how you can do this:

You can directly override the topmenu.phtml file in your theme directory (app/design/frontend/package/theme/template/page/html/). However, this is not a best practice as it requires maintenance whenever Magento updates occur.

In your overridden file, you could use PHP to manipulate $_menu before echoing:

<ul id="nav">
    <?php
    $_menu = str_replace('<li class="level0 nav-1 first">', '<li class="custom-class">', $_menu);
    echo $_menu;
    ?>
</ul>

This example replaces the class attribute of each <li> tag with a custom class.

A cleaner approach is to extend the core layout and add your own block that handles the menu:

  1. In your theme’s layout XML file (app/design/frontend/package/theme/layout/local.xml), define a new block:
<layout version="0.1.0">
    <default>
        <reference name="topmenu">
            <action method="setTemplate" template="page/html/topmenu.phtml" />
        </reference>
        <reference name="topmenu">
            <block type="custom/topmenu" before="-" template="page/html/custom_topmenu.phtml"/>
        </reference>
    </default>
</layout>
  1. Create the custom_topmenu.phtml file in your theme’s template directory (app/design/frontend/package/theme/template/page/html/). In this file, you can manipulate $_menu as needed before echoing:
<ul id="nav">
    <?php $_menu = str_replace('<li class="level0 nav-1 first">', '<li class="custom-class">', $_menu); ?>
    <?= $_menu ?>
</ul>

Conclusion

In Magento, the top menu HTML is generated by the core topmenu.phtml file. To change the <li> classes of these menu items, you have two options: override the core file directly (not recommended) or use layout XML to extend and manipulate $_menu in your theme.

Remember that hard-coding changes to core files can lead to maintenance issues during updates. Using layout XML for extension is a more modular and maintainable approach.

For more detailed guides on Magento development, I recommend checking out the resources at https://laravelcompany.com/magento-development.

Themes Admin

Our network