steve_b
steve_b

Reputation: 23

Magento 1.9: How to get Subcategories of specific Category?

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.

Upvotes: 2

Views: 6918

Answers (1)

Dhaval Patel
Dhaval Patel

Reputation: 1096

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(); 
  }
?> 

Upvotes: 3

Related Questions