Almazik G
Almazik G

Reputation: 1077

Magento. Determine if category is a top level category

I have a collection of categories $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->getItems(); and then I'm filtering through the results and displaying category thumbnail, name and link on the page.

<?php foreach ($categories as $cat): ?>
<ul>
    <li>
        <img src="<?php echo $cat->getImageUrl() ?>" alt="image">
        <a href="<?php echo $cat->getUrl()?>"><?php echo $cat->getName(); ?></a>
    </li>
</ul>

I need to do that ONLY for top level categories. So neither root category nor sub-categories should be included into the results set.

if I use magento's helper method to get top level categories then it doesn't provide me an access to all that methods like getImageUrl and getUrl that I need

Upvotes: 1

Views: 894

Answers (1)

Amit Bera
Amit Bera

Reputation: 7611

Just add filter by level by 2

    Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')->addAttributeToFilter('level', 2);

Upvotes: 1

Related Questions