Magento: Displaying Categories and Current Subcategories

I started “skinning” Magento today with a design that I was given. The functionality called for was a left category navigation (on every page but customer pages and cart/checkout pages) that always displayed the main categories – but when you click on a category, Magento should take you to that category’s listing, but also display the current subcategories.

So – since we all know that the Magento documentation is pretty crappy at this time, I had to do a lot of digging through core files, and a lot of time just with trial and error. But, I finally did come up with something that works. I’m willing to bet that there is a better, more proper way to do it, but regardless, this seems to be working perfectly.

What I did was create a new PHTML file, created the proper block call in page.xml, and now I’ve got a great working (and looking!) left-navigation. Here’s the home page (left), and a category page (right):


And the code:

<h3>Browse by Category:</h3>
$obj = new Mage_Catalog_Block_Navigation();
$store_cats	= $obj->getStoreCategories();
$current_cat 	= $obj->getCurrentCategory();
$current_cat	= (is_object($current_cat) ? $current_cat->getName() : '');
foreach ($store_cats as $cat) {
	if ($cat->getName() == $current_cat) {
		echo '<li class="current"><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a>\n<ul>\n";
		foreach ($obj->getCurrentChildCategories() as $subcat) {
			echo '<li><a href="'.$this->getCategoryUrl($subcat).'">'.$subcat->getName()."</a></li>\n";
		echo "</ul>\n</li>\n";
	} else {
		echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a></li>\n";

If you know of a better way to do this – please let me know!

This entry was posted in Magento. Bookmark the permalink.

26 Responses to Magento: Displaying Categories and Current Subcategories

  1. Jatin says:

    Hello Frends Any one can help me?? i hv problem.i wont to display category and sub-cateogory in drop down.As like from category i selected particular category the related to tht category ,sub category must be dispplayed???I have written this code but its not happeing.Please help

    (Sorry, your code did not come through in the comment properly)

  2. Tim Lu says:

    I can’t get subcategory,thank you

  3. Spawn says:

    for magento 1.5 I think ti retrieve the url you should use getUrlPath() function.

  4. Manuel says:

    Is it possible to get all subcategories from a specific category like category id = 5001??

    • Josh Pratt says:

      Manuel – Yes, it is, but I don’t have time to try and give you an example of how to do that right now. Dig through the Models that deal with categories in the Catalog core module.

  5. FlikstRR says:

    is there anyway to grab the Category Image too? I try $_imageUrl = $subcat->getImageUrl();

    and its not working :(

  6. Deo says:

    foreach ($this->getStoreCategories() as $_category):
    echo $this->drawItem($_category);

    • Fida says:

      i have a page home.phtml which will be displayed after cms on home page.In it I would like to display the categories.but when i copied the above code it is not working. Do i need to add anything in XML page . i am new to Magneto ?

  7. rahul says:

    usefull code thanks….. but its not working for 3tier…how can i display morethan 2tier?

  8. boris says:

    don¨t work subcategory 3RD+ LEVEL(((

  9. Dick says:

    Any ideas on how to make all the subcategories display on the homepage and not just on the top-level category pages? Thanks!

  10. @gast + @amanda
    The links will only work if you include this code in a block with type=”catalog/navigation”.

  11. Amanda says:

    I also cannot retrieve the URL, the href ends up blank: href=””

  12. Joshua says:

    thx, it will be my better project to solve the promble!

  13. May says:

    It’s good to display 2-tier categories. But how to display more than 2-tier subcategories like this?

  14. David says:

    Thanks for your code!

  15. gast says:

    I can’t get the categories URL

  16. Onyx says:

    it works perfect! thank you! just what I was looking for. magento should have this by default… LOL

  17. Marc says:

    Good stuff. Magento is powerful, weak on documentation, and marginally bloated. But until I build or find better I am all in.

  18. Adeel says:

    use full code thanks to Magento

  19. Marius says:

    I looks good, but what if I want to overwrite the Mage_Catalog_Block_Navigation class?
    Then I won’t be able to use my custom methods in this code.
    A better way to initialize the block is
    $obj = $this->getLayout()->createBlock(‘catalog/navigation’);

  20. chris g says:

    cheers mate, you are a gun!

  21. Moni says:

    its working fine.. cheers!!

  22. Lerentech says:

    Great work! I’ve been looking for this. Thanks so much!

  23. shakir says:

    it helped me alot ….. great work buddy… 😉

  24. Mike says:

    Looks good, but if i click on an subcategory the menu is closing the subcategory:
    Take a look: