0

I would like to list only the first level child categories of the current category.

This is what is have right now:

$_category = Mage::registry('current_category')->getId();    
$_categories = $_category
                ->getCollection()
                ->addAttributeToSelect(array('name', 'image', 'description'))
                ->addIdFilter($_category->getChildren());

foreach ($_categories as $_category):
   echo $_category->getName();
endforeach;

The shop is using flat categories, and i have no idea how to show only the first level of child categories.

Been searching a lot but every script i use still show all the sub-sub categories instead only the sub categories.

EDIT:

Just to be clear

When the current page is:

Category (current page)

-- sub A <-- show this sub category
--- sub A sub
---- sub A sub sub

-- sub B <-- show this sub category
--- sub B sub
---- sub B sub sub

Category 

-- sub A (current page) 
--- sub A sub <-- show this sub category
---- sub A sub sub

-- sub B
--- sub B sub
---- sub B sub sub
  • first level means immediate children categories? – Amit Bera Apr 14 '15 at 10:12
  • Hi @AmitBera Yes this is right. I updated the initial question, please have a look – RoRo Apr 14 '15 at 10:25
  • $_category = Mage::registry('current_category')->getId(); should be $_category = Mage::registry('current_category'); – Amit Bera Apr 14 '15 at 10:26
2

use below code to get first level category

  $categories = Mage::getModel('catalog/category')
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addIsActiveFilter()
                ->addAttributeToFilter('include_in_menu','1')
                ->addAttributeToFilter('level','1');


    foreach($categories as $item)
    {
        echo $item->getName();
    }
1

For getting all immediate child categories try this

For getting get immediate active categories:

$_category = Mage::registry('current_category');    
$_categories = $_category->getChildrenCategories()
                ->addAttributeToSelect(array('name', 'image', 'description'))
                ;

foreach ($_categories as $_category):
   echo $_category->getName().'<br/>';
endforeach;

With Including inactive Children categories:

$_category = Mage::registry('current_category');    
$_categories = $_category->getChildrenCategoriesWithInactive($_category)
                ->addAttributeToSelect(array('name', 'image', 'description'))
                ;

foreach ($_categories as $_category):
   echo $_category->getName().'<br/>';
endforeach;
  • when i do this i get a empty page (without sidebar etc.) When i var_dump $_category it give me stuff back but when i var_dump($_categories); die(); it shows nothing. – RoRo Apr 14 '15 at 11:08
  • on which pages,are you trying this? – Amit Bera Apr 14 '15 at 11:11
  • for now on the catalog/category/view.phtml and will put it in a extension later – RoRo Apr 14 '15 at 19:02
  • then it should works... – Amit Bera Apr 14 '15 at 19:03
  • $_categories did not provide the data.you cam get data when use foreach loop – Amit Bera Apr 14 '15 at 19:23
0

You can use the following method to get the first level child categories of the given category. $_categories = $category->getResource()->getChildren($category,false); Where false in the argument is for recursive, so if you want the recursive categories, you can replace the false with true.

0

You can use this way

$cat_id = Mage::registry('current_category')->getId();
$sub_categories = Mage::getModel('catalog/category')->getCategories($cat_id);

or you can also filter the category collection by parent id


$cat_id = Mage::registry('current_category')->getId();
$sub_categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToFilter("parent_id",$cat_id);

For me i prefer first method hope this will help you

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.