I'm using this code to show subcategories on the category page. I was working fine in but is broken in Does anyone have the updated code?

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
        ->addAttributeToSelect(array('name', 'image'))
        ->addAttributeToFilter('is_active', 1)
<h2>Choose A Subcategory...</h2>
<ul class="subcategories">
    <?php foreach ($categories as $category): ?>
            <a href="<?php echo $category->getUrl() ?>"><img src="<?php echo $category->getImageUrl() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" /><br />
                <span><?php echo $category->getName() ?></span></a>
    <?php endforeach; ?>
<span class="clear"></span>

Thanks for the help, but it turned out to be related to a bug in Magento http://www.magentocommerce.com/bug-tracking/issue/index/id/870

  • @brentwpeterson Are you running a "no thanks" bot?? – benmarks Nov 14 '15 at 2:17
  • @benmarks SE adds that text automagically. Should I be doing something different when reviewing posts? – brentwpeterson Nov 14 '15 at 13:17
  • @brentwpeterson Just wondered bc that wasn't a "thank you" answer. – benmarks Nov 15 '15 at 7:34
1 working code, goes as deep as you permit it. Give it a try. I also put some example filters in there for others who read this post.

$collection = Mage::getModel('catalog/category')->getCollection()

function recursion($catId) {
    $cat = Mage::getModel('catalog/category')->load($catId);

    if ($subcats = $cat->getChildren()) {
        foreach(explode(',',$subcats) as $subCatid) {
            $_category = Mage::getModel('catalog/category')->load($subCatid);
            // put your display code here
            // maybe also "recursion($subCatid);"
            // or "if($_category->getLevel() == 3)"
            // etc...

foreach($collection as $cat) {
    $catId = $cat->getId();

Also try:

$currentCategory = Mage::registry('current_category');
  • Thanks - that's put me on a good path, but how do I restrict it to one level deep? – ljc Nov 3 '15 at 17:12
  • I altered the answer to go only one level deep. – Stuperfied Nov 3 '15 at 22:15
  • You may also wish to add a check for $_category->getIsActive() – Stuperfied Nov 3 '15 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.