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.


Interesting point… in the new 1.4.1 they’ve depreciated the drawItem function and now it’s lovely and different
Hello,thanks for the article.I wonder is it possible if i want to remove the links for the level 0 and level 1 category link?So if the user clicks on the level 0 and level 1 category,it remains on the current page..Thanks a lot…
Hi Handoyo,
This should be possible – as long as your level 0 and level 1 category links have sub-categories in them. What you should try is to remove the && $level != 0 from the code – this should then make all categories with children “non-clickable”.
hmm, well I did as you said and nothing changed..would you possibly know why?
This would be the perfect solution for me, as my header is a category title (homewares), and then I put products into their relevant subcategory (cups)(plates). I don’t have (or want) any products in the main category as customers can select the subcat they are most interested in.
Cheers
I also need to remove links from category titles when the category has sub-categories. I’m using 1.4.2 and the Navigation.php has completely changed.
Has anyone managed to remove these links in 1.4.2?
Cheers
Hi Ben,
I have done the following with the new Navigation.php you can try adapting it: what this basically does is takes the links out the level0 categories:
Just below the line in the function _renderCategoryMenuItemHtml:
$htmlLi .= '>'; $html[] = $htmlLi; // below this if(stristr($htmlLi, "level0")){ // if level0 exists in this particular LI $html[] = '<a href="javascript: void(0);"'.$linkClass.'>'; // non-link } else { $html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>'; // put your normal stuff in } Hope this gets you on the right track!Great thanks for the reply.
Will this take off all top level links including one’s without children?
As I have categories with children and some without :s
Cheers
Hi Ben,
Yes it will – but you can do some checks in there – such as check if hasChildren etc and is level1 – this may stop that happening.
Hi,
I did what u said and now the menu and submenu dont have links, they stay on the same page. What I am trying to achieve is to dont have links on the menu (main categories) but have links on the submeniu.
if($level == 0 ){
$html.= ”.$this->htmlEscape($category->getName()).”.”\n”;
} else {
$html.= ‘getCategoryUrl($category).’”>’.$this->htmlEscape($category->getName()).’‘.”\n”; }
I tried even this, dont work:(
Any idea? thank you
grt!! I was looking for this for the past two days! Works fantastic! Thanks a lot!