Display Categories and SubCategories in Magento

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 &amp;&amp; $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; ?>

34 thoughts on “Display Categories and SubCategories in Magento”

  • Jona

    Hi, i suggest to to check if category is active and (from 1.4.1) if it can be displayed in navigation ;)

    Reply
  • Matt Martin

    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!

    Reply
  • Vinny

    Very nice! It is exactly what I needed. Thanks!

    Reply
  • TOM

    How do you get the categories to then paginate?

    Thanks

    Reply
  • tim

    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?

    Reply
  • Tonio

    Looks interesting, but how does it work =( ?

    Reply
  • sanjay

    Can you please specify the exact file with full path ?

    Thanks.

    Reply
  • Gaurav

    Good job....
    But i need n step subcategories listing can u help me..

    Reply
  • Alta

    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.

    Reply
  • DarkStar

    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

    Reply
  • Chris

    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

    Reply
  • Amatz Fox

    this is what i'm looking... thanks many much

    Reply
  • zuber

    Thanks buddy............This is same thing what i need for my project thanks again

    Reply
  • Terry

    Thanks buddy! Nice. It works fine

    Reply
  • [...] 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 [...]

    Reply
  • Wadii

    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.

    Reply
  • Matteo

    For those looking to display sub-sub-sub(weeeee!) categories in magento 1.5 + , check my snippet, works fine

    http://codepad.org/oncbuVRH

    Reply
  • lynda

    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?

    Reply
  • Ahmad

    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?

    Reply
  • sankha

    Its a real help..Thanks...U can put it any where.Please continue this...

    Reply
  • yusuff

    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

    Reply
  • Laura Lock

    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!

    Reply
  • tejpal navadiya
    tejpal navadiya April 4, 2012 at 6:30 am

    Hi,
    Display Top Level Categories Only i have use but all categories display.
    i want to 4 categories display in home page.

    Reply
  • Zeynep Erdogan
    Zeynep Erdogan May 24, 2012 at 9:55 am

    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!

    Reply
  • Arief Widyananda

    Thanks a lot! Very helpful for me as a newbie programmer on Magento :)

    Reply
  • Justin Liu

    Awesome! Cheers. One question: Is it possible to order subcats by ascending name?

    Reply
  • Jodie Eckford

    Awesome tutorial - thank you!

    I have a slightly different scenario - do you know whether it's achieveable?

    I have 2 x levels of categories which are displayed at all times, however I also have a 3rd level category which only needs to display when the it, a sibling or the direct parent is active.

    Is this possible?
    Thanks

    Reply
  • rakesh kumar

    Display Top Level Categories and ALL Subcategories
    I want to show product count of subcategory with status and visibility filter.
    Right now it is counting all the products present in the subcategory.

    Reply
  • Geoff Jackson

    Great, clean code. Thanks.

    How can this be modified to include categories that are set to not be included in the navigation menu from the Magento back office? Thanks

    Reply
  • Visvanathan

    How Can i get subcategories thumbnail image? thanks

    Reply
  • Triveni

    This code gets only top categories i need all the active categories . Can you please help ASAP

    Reply
  • Chintan

    Did anyone find a way to show images, thumbnails and custom attributes?

    Reply
  • Prignesh

    i have made one category in wordpress its "press".

    i need to show only "press" categories post in magento seprate cms page so please help me to do this thing done.

    Reply
  • AeroDigital

    Hi there, thanks for the article there's quite a few handy bits of code in there. I'm using the first one to create a responsive navigation menu on a Magento store. Thanks!

    Reply
Leave a Reply