2

I am using the following block in my Magento CMS for home site:

{{block type="catalog/product_list" name="catalog_list" category_id="1420" template="catalog/product/listStart.phtml"}}

How can I just get an output of all subcategories of the category_id as specified in the block (id 1420 in this case).

So far I have the following code:

<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<div class="category-products">
<div id="carousel">
    <ul class="products-in-row">
        <?php $i=0; foreach ($collection as $cat): ?>
            <li class="item">
                <?php echo $cat->getName();?>
            </li>
        <?php endforeach ?>
    </ul>
</div>

I'm getting all subcategories of the main category only.

steve_b
  • 23
  • 1
  • 4
  • Possible duplicate of [Magento - get a parent category and all sub-sub-categories](http://stackoverflow.com/questions/5564647/magento-get-a-parent-category-and-all-sub-sub-categories) – Douglas Radburn Jan 07 '16 at 16:44
  • @DouglasRadburn - different issue. This issue in this question is not being able to apply the value `1420` from `category_id` to the collection as far as I can tell, not how to do it in general. It's also a different scenario to the question you've linked to. – scrowler Jan 08 '16 at 00:56

1 Answers1

3

This code may help if you want to get child category of every current category

  <?php
  $layer = Mage::getSingleton('catalog/layer');
  $_category = $layer->getCurrentCategory();
  $currentCategoryId= $_category->getId();
  $children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
  foreach ($children as $category)
  {
      echo $category->getName(); // will return category name 
      echo $category->getRequestPath(); // will return category URL
  }
  ?>

Another way:

<?php
  $categoryId = 10 ;  // get current category id
  $category = Mage::getModel('catalog/category')->load($categoryId);
  $catList = explode(",", $category->getChildren());
  foreach($catList as $cat)
  {
     $subcategory = Mage::getSingleton('catalog/category')->load($cat);
     echo $subcategory->getName(); 
  }
?> 
Pushpendra Singh
  • 182
  • 1
  • 1
  • 13
Dhaval Patel
  • 1,076
  • 6
  • 19