Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - Display only 'root category' in top menu

Tags:

php

magento

I am trying to display a 'products' menu item in my top menu then have all the subcategories displayed underneath it in a dropdown.

I am looking at the code in the top.phtml file and I just can't figure out how to configure it to display the root category with all categories underneath it.

This is the current code that pulls the categories and subcategories:

 <?php foreach ($this->getStoreCategories() as $_category): ?>
        <?php echo $this->drawItem($_category) ?>
    <?php endforeach ?>

Anyone have an idea of how I can display only the root category as a menu item (ie: "Products") then display all the subcategories (and their subcategories) beneath it?

Thanks.

like image 909
cantaffordavan Avatar asked Feb 11 '12 21:02

cantaffordavan


1 Answers

This is a pretty common question and it probably exists out there already. This should get you started:

  <?php

  $root_category = Mage::getModel('catalog/category')->load(3); // Put your root category ID here.
  $subcategories = $root_category->getChildren();
  foreach(explode(',',$subcategories) as $subcategory) {
        $category = Mage::getModel('catalog/category')->load($subcategory);
        echo '<a href="'.$category->getURL() .'" />'.$category->getName().'</a><br/>';
  }

  ?>
like image 78
seanbreeden Avatar answered Oct 04 '22 12:10

seanbreeden