Blog

Magento Exclude Categories From Top Category Menu Navigation

Magento Exclude Categories From Top Category Menu Navigation

Magento is arguably the most robust and advanced open-source e-commerce system available in the marketplace today, it is not however without it’s little annoying traits and as you know it’s the little things that lift that matter. One thing that Magento does not easily allow you to do as yet is hide a category in the top navigation but still show it in the sidebar navigation, luckily this tutorial will show you exactly how to acheive this feature by editing your template file for the top category navigation bar.

This particular method will allow you to exclude individual categories by ID number from you top navigation menu in Magento.

Firstly depending on which theme you are using for magento you will want to locate the top.phtml which is the file that requires editing to exclude individual categories. You can find the original copy of this file in /app/design/frontend/base/default/template/catalog/navigation/top.phtml.

Copy the top.phtml file to the theme you are using to style you magento store place it in the /app/design/frontend/default/YOURTHEMENAME/template/catalog/navigation/top.phtml folder. If the folder “navigation” does not exist within the catalog folder in your theme simply create it.

Now open the top.phtml file with an HTML / PHP friendly text editor.

The original contents of the top.phtml file should look like below.

<?php $_menu = $this->renderCategoriesMenuHtml(0,'level-top') ?>
<?php if($_menu): ?>
<div class="nav-container">
    <ul id="nav">
        <?php echo $_menu ?>
    </ul>
</div>
<?php endif ?>

Replace the code above with this code below.

<?php $_menu = ''?>
<?php foreach ($this->getStoreCategories() as $_category): ?>
    <?php $_menu .= $this->drawItem($_category) ?>
<?php endforeach ?>
<?php if ($_menu): ?>
<div class="nav-container">
    <ul id="nav">
    <?php foreach ($this->getStoreCategories() as $_category): ?>
    <?php if (!in_array($_category->getId(), array(12,34,56))) : ?> <?php echo $this->drawItem($_category) ?>
    <?php endif; ?>
    <?php endforeach ?>
    </ul>
</div>
<?php endif; ?>

Now to exclude categories from your top navigation menu you can simple enter the category ID number you wish to exclude where the example numbers 12,34,56 are in the code above.

To find out the ID of a category go to your Magento Admin panel and Categories, when you click to edit a category at the top left you will see it’s numerical ID.

This trick can be particular useful for designers when the client wants more categories than the top navigation menu will allow. The excluded categories still show up in the main sidebar navigation menu of course as we have made no edits to that. This may not be the most elegant of solutions i.e. you can only exclude the categories by editing a template file for building custom themes though it can be a life saver.

27 Responses

  1. Doesn’t work for Magento 1.4.0.1 I’m afraid :(

    Any ideas?

    • Hi Michael we have this code running on an live install of both v1.4.0.1 and v1.5.0.1 of magento so we can confirm that it works on both of these versions.

  2. Thanks worked a treat on 1.5!

  3. I found that I had to clear the cache and then it worked.

  4. I need to remove or preferably place the product category bar that is at the top of the home page down lower on the home page or off the “home page only” all together leaving it where it is at on all other pages…..

    Thoughts?

    Can you help?

  5. not working in 1.4.1.1 . any idea? thanks

    • It should work in v1.4.1.1 carlagnas? unfortunatley it not working could be caused by many things, too many to narrow down without more detailed information from yourself.

  6. This was fantastic! Exactly what I needed and so easy to install!!!

  7. Not working for me under 1.5.1.0 either. Strange, cleared cache numerous times and reindexed twice.

  8. Awesome ………………………….!!!!!!!!!!!
    You did a great job …………

  9. Is there a way to perform this but to sub-categories of TOP categories? in fact what im trying to do is to check if the SUB-category have products inside or not…

  10. hi, can you help me, this is exactly what I need but cant get it to work, magento 1.6, I think I have done it right, my updated code is below, it removes all the categories except the Home link, any help would be great!

    Cheers

    * Top menu for store
    *
    * @see Mage_Catalog_Block_Navigation
    */
    ?>

    <a href="getUrl(”) ?>”>Homepage

    getStoreCategories() as $_category): ?>
    drawItem($_category) ?>

    getStoreCategories() as $_category): ?>
    getId(), array(163))) : ?> drawItem($_category) ?>

    getChildHtml(‘cart_menu’) ?>

  11. ?>

    <a href="getUrl(”) ?>”>Homepage

    getStoreCategories() as $_category): ?>
    drawItem($_category) ?>

    getStoreCategories() as $_category): ?>
    getId(), array(163))) : ?> drawItem($_category) ?>

    getChildHtml(‘cart_menu’) ?>

  12. wont let me paste the code, does it work on 1.6?

  13. Hi Stuart, thanks very much for this. It works perfect in 1.6.0.0 too!, we’ve used this in a couple of themes and it works very fine.

    Cheers!.-

  14. This worked for me in Enterprise v1.11.1.0

  15. thanks for this btw…if you are using the default theme the correct path is \app\design\frontend\base\default\template\catalog\navigation

  16. It obviously works, but fixes like this create spaghetti code. Unfortunately this is not preventable since Magento really, really sucks. What about a simple checkbox called ‘visible in menu’ in the edit-category page. Noooo that would be too much work for the Magento developers, better spend our time developing a newsletter system that nobody’s gonna use..

  17. works perfectly in Magento 1.6.1. Thanks for the help! This is great!

  18. I would like to konw how to add a div tag to the menu right before the second ul Is that possible? I can’t seem to find any place that has this info.

  19. Thanks for it, i wanna like exclude first category but show all it subcatgory in top menu navigation , how we can get it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*