Magento Custom Category Listing Block

Here I wanted to show you how to build a custom Magento category listing block that you can use on your own Magento store. We will go over each step at getting a category listing of your magneto categories and even a sub category listing.

Here is the finished code for your new category listing block.

$cats = Mage::getModel('catalog/category')->load(2)->getChildren();
$catIds = explode(',',$cats);
<?php foreach($catIds as $catId): ?>
			$category = Mage::getModel('catalog/category')->load($catId);
			echo $category->getName();

			$subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
			$subCatIds = explode(',',$subCats);
            <?php if(count($subCatIds) > 1):?>
                <?php foreach($subCatIds as $subCat) :?>
                        $subCategory = Mage::getModel('catalog/category')->load($subCat);
                        echo $subCategory->getName();
                <?php endforeach;?>
            <?php endif; ?>
<?php endforeach; ?>

And The XML Block Code:

<block type="catalog/navigation" name="catalog.category" template="catalog/navigation/category.phtml" />

After loading each category you have a whole array of different attributes you can use. If you wanted to make each category listing be a link, simply use the getUrl() function in a similar fashion:

<a href="<?php echo $category->getUrl()?>">
	<?php echo $category->getName()?>

Enjoy everyone!
Devin R. Olsen

Devin R. Olsen

Devin R. Olsen

Located in Portland Oregon. I like to teach, share and dabble deep into the digital dark arts of web and game development.

More Posts

Follow Me:TwitterFacebookGoogle Plus

58 Responses to “Magento Custom Category Listing Block”

  1. Devin R. Olsen Mike says:


    Thanks for the tutorial. This has been a helpful example.

    Is there a way to alphabetize the categories? If so, can you please share that with us? It would make this addition much more sustainable for the long term.



  2. Devin R. Olsen jayR says:

    i was hoping you can tell me how to sort the list in alphabet order :) Great code but to make it perfect we need sort-order

    Cheer :)

  3. Devin R. Olsen jay says:

    Hi, how would I go about sorting by position in the admin panel? currently it is sorted by id


  4. Devin R. Olsen Tim says:

    Devin, thanks for your time in getting this together.

    However, as I was working on a similar but slightly different block I noticed a function that eases the process a bit.

    After you have the default cat loaded (“…->load(2)”), instead of calling getChildren() to get the ids as a string, you can simply call getCollection(). This returns an array of models for you to loop through.

    You’re way works just fine, and I thank you again! This is just one less step, and also less overhead. However, we are dealing with Magento so it wouldnt be noticed anyay. Haha!



  5. Devin R. Olsen Luca says:

    Sorry, I made an error in copying the code. The correct one is:

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


  6. Devin R. Olsen Luca says:

    Hi Devin and thank you so much for the post!!

    Only one question. Where do I need to put the code below?

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

    Thank you so much!!!

  7. Devin R. Olsen Michel says:

    Hello Devin!

    I put menu categories in top e subcategories in left.
    I need this:
    Clotes (Categories Top)
    sweaters(subCategories left)

    Acessories (Categories Top)
    rings (subCategories left)

    can you teach me please…
    I make this …

    $catIds = explode(‘,’,$cats);

    $subCats = Mage::getModel(‘catalog/category’)->load($category->getId())->getChildren();
    $subCatIds = explode(‘,’,$subCats);


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

  8. Devin R. Olsen Mattia says:

    Have found: $this->isCategoryActive($category)

  9. Devin R. Olsen Mattia says:

    And how to retrieve if the category is active? same to topnavigation..

  10. Devin R. Olsen fes says:

    hmm, the echo’s are being stripped from the code but i think you guys can figure that one out yourselves :)

  11. Devin R. Olsen fes says:

    Thanks for your code! I hope you don’t mind but i’ve created a recursive version with a dynamic root category:

    function renderChildren($category) {
    $children = Mage::getModel(‘catalog/category’)->load($category)->getChildren();
    if(!empty($children)) {
    $categories = explode(‘,’, $children);

    echo ”;
    foreach($categories as $categoryId) {
    $category = Mage::getModel(‘catalog/category’)->load($categoryId);
    echo ”;
    echo ‘getUrl() .'”>’. $category->getName() .’‘;
    echo ”;
    echo ”;


    This way you can use it with multiple root categories and an infinite depth.

  12. Devin R. Olsen Otto Tarella says:

    Good blog, keep me personally from searching it, I’m really interested to learn much more about it.

  13. @Marko Väljaots,
    Yeah I am well aware, but for some reason while building this tutorial I found it not working with me unless the condition was greater than 1 vs. the logical greater than 0.

    If someone could please confirm for me that their results come out clean with the logical greater than 0 condition that would be helpful.

  14. Devin R. Olsen Marko Väljaots says:

    And thanks for the great article!

  15. Devin R. Olsen Marko Väljaots says:

    There is a small mistake on line 17. subCatIds should be greater than 0, not greater than 1.

    For example:


  16. Devin R. Olsen Kamwing says:


  17. Devin R. Olsen Sven Aarts says:

    Thanks for this tutorial! I appreciate your work.

  18. Devin R. Olsen Ingwa says:

    Devin, thanks so much! This code was really helpful to figure out Magento’s theming structure and layout files. The thing that I was running around in circles with was due to the fact that when I created my theme, from scratch mind you, I had defined the Templates and Skin directives under System > Config > Design > Themes but had not done the same for translations, layout or default. It’s imperative that these are all set to the theme name otherwise it’s an endless loop of not seeing changes. You’ve saved me my sanity. Thanks.

  19. Devin R. Olsen Nellie says:

    This didn’t work for me. Is this for 1.4 only?

  20. Devin R. Olsen Bart says:

    Devin, nevermind, I found an extension that does the job perfectly. Thanks anyway!

  21. Devin R. Olsen Bart says:

    Hi Devin, thank you so much for your explanation about this. Really helped me. Question: Is there a way to only load the subcategories of the category you are in at that moment?
    So i have category A, with subs A1 and A2. If i’m IN category A, i only want to show subs A1 and A2 and not the other main categories (B, C, D etc.). Or does this require major modifications to the php?

  22. Thanks paul! This has promptly been corrected!

  23. Devin R. Olsen paul says:


    You made a typo in the block reference..

  24. Devin R. Olsen Mizpah says:

    A huge help, especially hearing the commentary as you built the file up, rather than simply reading the end result!

    Looking forwards to more of this quality :)

  25. I always enjoy feedback from my readers.

Leave a Reply