Customise Navigation Menu Links
The navigation menu in magento is more versatile than it seems. Obviously the menu does what it needs to do – list categories and subcategories in their respective positions. However recently I got a request to manipulate the menu and make some things click-able and others not click-able.
The client wanted only the landing pages to be links – this meant that any list item with a drop down list appearing off of it would not be click-able – however all the ending pages – such as the subcategories had to be linked. Another interesting one was that they still wanted the main categories to be links – even though they have drop downs coming off them…strange huh?
I’m not quite sure what the purpose of this exercise was but apparently it made more sense to their e-commerce web design if the menu acted this way, however bad it may seem for SEO purposes.
I thought that this could be useful for some people so I have decided to share it here on our magento blog. The magento fox may even tweet about this too.
First of all I found the core code that needs editing – the drawItem() function. You can find this in the app>code>core>Mage>Catalog>Block>Navigation.php.
As always you should make a local copy of this file so that when upgrading magento your changes are not undone.
In Navigation.php you will find the function drawItem() all you have to do is replace the part that includes the <anchor tag> with a version with no <anchor tag>. The tricky part is determining which items get the links and which do not.
Find this line in your code:
$html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";
And replace with:
if($hasChildren && $level != 0){
$html.= '<span>'.$this->htmlEscape($category->getName()).'</span>'."\n";
} else {
$html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";
}
This should make immediate sense to any magento user – basically if the level is not 0 (i.e NOT the main categories) and also if it has children (has drop downs coming off) then don’t add an anchor tag around the text. Something that I thought would be really hard to achieve was actually really easy and completed in 5 minutes.
If you found this blog useful please leave a comment and let me know, I’d love to hear the situations where you would need to use this code because at the moment, just like that darn magento fox, it’s eluding me.





- this code outputs ALL of your products – I tried it on a store with 750 products on and it took 20 seconds to load the page. Use this as a rule of thumb – however I would recommend adding a form of pagination to this code and also limiting the sql array to something smaller like 50 per page.