
A newer, better version of this post has been written.
Category Navigation Listings in Magento eCommerce
The majority of Magento websites out there list their top level categories as well as the current categories sub-categories. This feature is commonly requested on forums so I decided to write a small post about it.
Rather than just write out the code, I will show you a few variations so that you can get the right one for you.
All of the following code samples can be copy and pasted into ANY template file and will function correctly.
Display Top Level Categories Only
<?php
/*
* http://fishpig.co.uk - Magento Tutorials
*
* Display top level categories
*
**/
?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Display Top Level Categories and ALL Subcategories
<?php
/*
* http://fishpig.co.uk - Magento Tutorials
*
* Display top level categories and subcategories
*
**/
?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Display Top Level Categories and Current Categories SubCategories
<?php
/*
* http://fishpig.co.uk - Magento Tutorials
*
* Display top level categories and
* subcategories of the current category
*
**/
?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>








Hi, i suggest to to check if category is active and (from 1.4.1) if it can be displayed in navigation ;)
Works great (I used the parent & subcategory display). I tweaked it to get it to indent the subcategories. Thanks for sharing this–I wish I found it sooner…it would have saved me a lot of frustration. But, it saved me more time–thanks!
Very nice! It is exactly what I needed. Thanks!
How do you get the categories to then paginate?
Thanks
Great site fishpig. It works. But with different outcome than I thought. When on top level categories it does display sub categories BUT will not when you are actually in one of the sub categories. When you are in a sub category, it only displays the top level categories.
I was hoping for a solution that I could rig to only display other sub categories in the parent cat only. Any ideas?
Looks interesting, but how does it work =( ?
Can you please specify the exact file with full path ?
Thanks.
Good job....
But i need n step subcategories listing can u help me..
What version of Magento is this for?
If everyone would post the version when discussing code, it'd make life a lot easier. I don't know how many hours I wasted trying the wrong version code because people don't post the version. It should be a federal offense to not post the version.
It only takes a minute to fall in love...
...it also only takes a minute to copy the code into a file and see if it works with your version of Magento
Hi,
thats cool...
I need to get the Description for every category.
Not with Category-ID, like this code at here but with one more description
can u help me????
regards from German
this is what i'm looking... thanks many much
Thanks buddy............This is same thing what i need for my project thanks again
Thanks buddy! Nice. It works fine
[...] und auch verschiedene Storeviews funktionieren, der kann mit dieser Lösung arbeiten, die ich unter http://fishpig.co.uk/display-categories-and-subcategories-in-magento/ gefunden [...]
Good tutorial!
What i'm tryin to come up with me , is how to display all categories and sub-*-catagories in one level , Means One root catégory , you mouseover , and you can see ALL the categories at once.
For those looking to display sub-sub-sub(weeeee!) categories in magento 1.5 + , check my snippet, works fine
http://codepad.org/oncbuVRH
This code works great, but my store has over 3000 categories and it renders rather slowly. Is there another way to extract cats and subcats quickly?
Your code work fine but I want it expandable just like the jquery tree. So that when I click on a category it should expand and display its sub categories. How will I do this?
Its a real help..Thanks...U can put it any where.Please continue this...
wow super,, i got solution,,through this post....
help me how to diplay the top menu bar in magento by the use of externel code..]
thank in advance
Wow! Thanks, yours is the first code that I can easily understand and literally read, copy, paste, save, upload, DONE.
Thank you! Keep doing what your doing!
Hi,
Display Top Level Categories Only i have use but all categories display.
i want to 4 categories display in home page.
Hi!
It is really nice share! Thank you for your helps!
It works find But is there anyway to list them vertically?
like;
category1 category2
sub1 sub1
sub2 sub2
I am not that good in php Can you please help me? Thank you!
Thanks a lot! Very helpful for me as a newbie programmer on Magento :)