Magento: Display sub-category list

doubleplusgood picture doubleplusgood · Nov 30, 2009 · Viewed 43.5k times · Source

I'm building a Magento store and want to be able to display a list of categories and have each category link to its own page.

I have a 'Brands' category with an ID of 42 and I want to display a list of the sub-categories and ensure that each one links to the designated URL key in the CMS.

Has anyone had experience of doing this with Magento?

Answer

wookiehangover picture wookiehangover · Dec 13, 2009

If you're comfortable editing your theme, this code snippet will bring you a list of all sub-categories of the current category (from the session, so this should work anywhere in your theme). I typically use this in app/design/frontend/default/theme_name/template/catalog/category/view.phtml

<?php
$_category  = $this->getCurrentCategory(); 
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper     = Mage::helper('catalog/category');
?>

<ul>
    <?php foreach ($collection as $cat):?>
            <?php if($_category->getIsActive()):?>
                <?php 
                     $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
                     $_img = $cur_category->getImageUrl();  
                ?>
                <li>
                    <a href="<?php echo $helper->getCategoryUrl($cat);?>">
                         <img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
                         <cite><?php echo $cat->getName();?></cite>
                    </a>
                </li>
            <?php endif?>
    <?php endforeach;?>
</ul>