Engine23
blogBannerImage
By Engine23

Magento - How to add to the topMenu using xml

When looking at the template files for Magneto, things can seem very confusing.  Especially when your talking about the navigation.  For example if you try to add a menu option to the top menu, you would expect to find an array or some sort of call to the database.  However, in app/design/frontend/default/YOURTHEME/template/page/html/header.phtml there is only a php code <?php echo $this->getChildHtml('topMenu') ?>.

Unless your savvy enough to understand whats happening, that is a reference to some xml.  This xml is what sets the values for that menu. 

So, how are we supposed to add things to that menu?  There are lots of bad advise out there saying you can just write your own, or get that list of values and hard code your own...but its so much easier than all of that.

The best way is to create or edit your local.xml

app/design/frontend/default/YOURTHEME/layout/local.xml

You are going to want to add this inside the <default> node in that xml document.

 <layout version="0.1.0">
    <default>
<reference name="top.links">
            <action method="addLink" translate="label title" module="blog"><label>blog</label><url helper="blog/getBlogUrl"/><title>blog</title><prepare/><urlParams/><position>12</position></action>
            <action method="addLink" translate="label title" module="test"><label>test</label><url>/test/me/out</url><title>test</title><prepare/><urlParams/><position>14</position></action>
        </reference>
</default>
</layout>



Now, in order for the blog url to work, you need to create a function in the Helper app/code/local/YOURNAMESPACE/Blog/Helper/Data.php called getBlogUrl.
That file could look something like:

<?php
class YOURNAMESPACE_Blog_Helper_Data extends Mage_Core_Helper_Abstract
{
    public function getBlogUrl()
    {
        return Mage::getUrl('blog');
    }
}

Now, if you are using the second example, then you are just coding the module/controller/action, so its a bit more simple, but you may run into a bad url at times, depending on how that path is executed. its much better to use that <url helper="blog/getBlogUrl"/> to make sure you have the proper Magento method to return a URL.