Get a certain category level

How can I get a certain category level from Magento, setting my category now looks like this.

root_catalog |-Shop |-Shoes |-T-shirts |-Brands |-Nike |-Womens |-Mens |-Adidas |-Asics <?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?> <?php echo $category->getName(); ?> <?php endif ?> 

When calling $ category-> getName (); I would like to show only the brand name, is this possible?

+7
source share
2 answers

You can get the category level from $category = Mage::getModel('catalog/category')->load( $categories[1]) )->getLevel() and then check your brand level if the match and display name.

eg. suppose the brand category level is 3

 <?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?> <?php if($category->getLevel() == 3) echo $category->getName(); ?> <?php endif ?> <?php endif ?> 
+8
source

ANKIT's answer is good, but it could be improved by actually requesting specific levels instead of loading the entire collection and fulfilling the conditional. Take, for example, if you want to get all categories at a certain level:

 <ul> <?php $categories = Mage::getModel('catalog/category') ->getCollection() // magic is prepared here.. ->addAttributeToSelect('*') // then the magic happens here: ->addAttributeToFilter('level', array('eq'=>2)) ->load(); foreach($categories as $category): ?> <li>$category->getName()</li> <?php endforeach; ?> </ul> 
+5
source

All Articles