MAGENTO getting the descriptions from a sub category


Hi I'm new to magento and have been trying to set up a static block that displays a list of sub categories within a category. I've been succesfull a grabbing the sub-category images and names, but for some reason I can't seem to get the descriptions to show.

Here's the code can't anyone explain why it won't work and how I can fix it?

I've commented out a few lines because I was trying different things to get it to work.

$category = $this->getCurrentCategory();



<?php foreach ($_categories as $_category): ?> <?php  echo 


        <?php if($_category->getIsActive()): ?>

            <div class="subcategory-image">

                        <a href="<?php echo $_category->getURL()

?>" title="htmlEscape($_category->getName())

                            <?php /* echo "Find this item->" */ ?>

                    </div> <div class="sub-category-container">
                    <h2><a href="<?php echo $_category->getURL()

?>" title="htmlEscape($_category->getName())

getURL() ?>"
getDescription() ?>–>


categoryAttribute($_category, $_description, 'description'); ?>


Best Solution

This is one of those cases where Varien decided that they should call "load" on the data collection before returning it when that really isn't necessary and makes the utility function utterly useless.. If you trace the code down for Mage_Catalog_Block_Navigation->getChildrenCategories() you will eventually find this in Mage_Catalog_Model_Resource_Eav_Mysql4_Category:

public function getChildrenCategories($category)
    $collection = $category->getCollection();
    /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        ->addAttributeToFilter('is_active', 1)
        ->setOrder('position', 'ASC')
    return $collection;

The next to last line ->load(); means that the query is executed and the collection is loaded so it is too late to modify the query. So what you will want to do is copy and paste that in place of calling getChildrenCategories and then add the additional attributes you want to use like so:

$_categories = $category->getCollection()
    ->addAttributeToFilter('is_active', 1)
    ->setOrder('position', 'ASC')

Now the collection will include the description attribute so that getDescription() will work. Notice that you do not need to call load(), it happens automatically when you start using the iterator (the foreach loop triggers this). This is why it is pointless for the load() call to be included in that function because otherwise you could have just added one line below the function call:


But instead you have to copy the contents of the function to tweak the query.

Related Question