简体   繁体   中英

Getting all Magento categories with their names

I'm trying to get all the categories in my Magento website. I've achieved that but they only come back with limited data. I need the name of the category which I'm missing. I've tried using addAttributeToSelect but that doesn't make any difference. Here is my code.

$cats = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->getData();
print_r($cats);

Your code is OK. Except the print_r .
You are printing a collection object and you need to print the items in it.

$cats = Mage::getModel('catalog/category')
         ->getCollection()
         ->addAttributeToSelect('*');//or use 'name' instead of '*'

foreach ($cats as $cat){
    echo 'ID: '.$cat->getId().' Name: '.$cat->getName().'<br />';
}

http://magentotutorialbeginners.blogspot.in/2014/03/magento-all-categories-with-their-names.html

$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url')
->addAttributeToSelect('is_active');

foreach ($categories as $category)
{
    if ($category->getIsActive()) { 
        $name = $category->getName();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM