doubleplusgood doubleplusgood - 4 months ago 28
PHP Question

Magento: Display sub-category list

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?

Thank you.


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

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

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