Faran Khan
Faran Khan

Reputation: 1565

Magento - how to filter categories in phtml

I have been working on the maga menu. I got the collection in phtml like:

<?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories() ?> <?php $currentCategory = Mage::registry('current_category') ?>

Now I have to add the filter to show specific categories. Like I have an array(1,2,3,4) of categories that i want to show. so how can I apply filter to this Helper.

Any one have any suggestion please answer.

Thanks.

Upvotes: 1

Views: 725

Answers (2)

Justin Paul Pa&#241;o
Justin Paul Pa&#241;o

Reputation: 936

The first answer is correct but it's not efficient as it consumes unnecessary database round trips. @Karan's code issues a query to the database for every id. Just imagine if the number of category ids to be filtered were 50 or above.

My example would be this:

<?php

$catIds = array(1,2,3,4);

$catCollection = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('id', $catIds)->addAttributeToFilter('is_active',1);

foreach($catCollection as $category){
  echo $category->getName()." ";
}

This will reduce the database roundtrip to just one.

Upvotes: 1

Karan Adhikari
Karan Adhikari

Reputation: 362

use this code

<?php $catids[]=array(1,2,3,4);

 foreach($catids as $id):

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

                     if($_category->getIsActive()):
                            echo $_category->getName();
                     endif;
endforeach;

?>

and don't forget to link my answer if it was helpful

Upvotes: 1

Related Questions