1

How can I get a collection of the categories and subcategories to build custom navigation ?

$_categories = Mage::getResourceModel('catalog/category_collection');

$i;

echo '<div class="navigation">';

foreach($_categories as $_category){

    echo '<ul class="category">';

    echo $i. '<li>'. $_category->getName().'</li>';$i++;

    echo $categoryId = $_category->getId();

    $_category = Mage::getModel('catalog/category')->load($categoryId);

//$category = Mage::getModel('catalog/category')->load(228);

    $subcategories = $_category->getChildrenCategories()->addAttributeToSelect('*');

    if (count($subcategories) > 0){

        echo $_category->getName();

        foreach($subcategories as $id){

            echo $name = $id->getName().'<br/>';
        }
    }
}
echo '</div>';`

It actually is giving me a full list of categories including all those I don't need ;) Should I pull all categories from products ?

2
  • Please add details which categories you don't want in your collection Mar 14, 2017 at 17:56
  • Actually i would like to have all categories and sub categories then i will seletcively show/hide what i want to see
    – Rob D. A.
    Mar 14, 2017 at 19:17

1 Answer 1

2

you can select sub categories based on categorie id also. If You need i can give you code also .

// here is the code

<?php 

$parentCategoryId = 222;
$cat = Mage::getModel('catalog/category')->load($parentCategoryId);
$subcats = $cat->getChildren();

// Get 1 Level sub category of Parent category
foreach(explode(',',$subcats) as $subCatid)
              {
                $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()) {
    echo '<li><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a><li>';

  }
}
*/
?>
4
  • Hello Saurabh, would be very nice. However I was doing it, just because collection contains all categories including brands, events etc not just products ... my menu is like 800 buttons :) and I don't know how to filter it :)
    – Rob D. A.
    Mar 15, 2017 at 12:12
  • <?php $parentCategoryId = 222; // change to your main cat id $cat = Mage::getModel('catalog/category')->load($parentCategoryId); $subcats = $cat->getChildren(); // Get 1 Level sub category of Parent category foreach(explode(',',$subcats) as $subCatid) { $_category = Mage::getModel('catalog/category')->load($subCatid); if($_category->getIsActive()) { echo '<li><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a><li>'; } } */ ?>
    – inrsaurabh
    Mar 15, 2017 at 12:19
  • Aaaaaa it's obvious! Silly me. You are 100% right, this is it!. I should read only a root for this range of categories what in result will give me an array with ids I'm looking for ;) Right ? :)
    – Rob D. A.
    Mar 15, 2017 at 12:22
  • Yes brother ! You can fetch $_category as you want .
    – inrsaurabh
    Mar 15, 2017 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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