A simple call to get image categories and display in a list

I am showing a list of subcategories by parent category id and want to show the category image instead of the category name.

Here's what I have so far ...

<div id="menu_brands">
<div class="brand_head">
    <h3><?php echo $this->__('Browse By Brand') ?></h3>
</div>
<div class="brand_list">
    <?php
        $cats = Mage::getModel('catalog/category')->load(6)->getChildren();
        $catIds = explode(',',$cats);

        $categories = array();
            foreach($catIds as $catId) {
                $category = Mage::getModel('catalog/category')->load($catId); 
                $categories[$category->getName()] = $category->getUrl();
                $img = $category->getImageUrl(); //I suspect this line is wrong
        }

        ksort($categories, SORT_STRING);
    ?>

        <ul>
            <?php foreach($categories as $name => $url): ?>
                <li>
                    <!--<a href="<?php echo $url; ?>"><?php echo $name; ?></a>-->
                    <a href="<?php echo $url; ?>" title="<?php echo $name; ?>">
                        <img src="<?php echo $img; ?>" width="auto" alt="<?php echo $name; ?>" /> <!--I suspect this line is wrong-->
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
</div>
</div>

      

I've tried countless ways to display images instead of category names, but nothing seems to show images. Currently with the above, the output is an empty "img src" so there is a clear error with what I am trying (and probably the best way to achieve what I need).

Please can someone kindly point out what the problem is?

If it makes any difference, then I will then display category images in a grid format (3 or 4 per line).

Thank you very much in advance.

+3


source to share


3 answers


We managed to fix this ourselves - see the fix below.

<?php
    //gets all sub categories of parent category 'Brands'
    $cats = Mage::getModel('catalog/category')->load(6)->getChildren();
    $catIds = explode(',',$cats);

    $categories = array();
    foreach($catIds as $catId) {
        $category = Mage::getModel('catalog/category')->load($catId); 
        $categories[$category->getName()] = array(
            'url' => $category->getUrl(),
            'img' => $category->getImageUrl()
        );
    }

    ksort($categories, SORT_STRING);
?>
    <ul>
        <?php foreach($categories as $name => $data): ?>
            <li>
                <a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>">
                    <img class="cat-image" src="<?php echo $data['img']; ?>" />
                </a>
            </li>   
        <?php endforeach; ?>
    </ul>

      



Surprised we didn't get more support on this as it is a relatively simple Magento issue. Thanks B00MER for your answer.

+4


source


Zigojacko's solution is not ideal because it is loaded separately in the model in a loop. It doesn't scale well and many categories will break up your database. Ideally, you add images to a child collection.

A faster solution is to add the image attribute to the collection with an ID filter like B00MER:



// Gets all sub categories of parent category 'Brands'
$parent = Mage::getModel('catalog/category')->load(6);

// Create category collection for children
$childrenCollection = $parent->getCollection();
// Only get child categories of parent cat
$childrenCollection->addIdFilter($parent->getChildren());
// Only get active categories
$childrenCollection->addAttributeToFilter('is_active', 1);

// Add base attributes
$childrenCollection->addAttributeToSelect('url_key')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('all_children')
        ->addAttributeToSelect('is_anchor')
        ->setOrder('position', Varien_Db_Select::SQL_ASC)
        ->joinUrlRewrite();

// ADD IMAGE ATTRIBUTE
$childrenCollection->addAttributeToSelect('image');

?>
<ul>
    <?php foreach($childrenCollection as $cat): ?>
        <li>
            <a href="<?php echo $cat->getURL(); ?>" title="<?php echo $cat->getName(); ?>">
                <img class="cat-image" src="<?php echo $cat->getImageUrl(); ?>" />
            </a>
        </li>   
    <?php endforeach; ?>
</ul>

      

+8


source


Try using this method instead of $img = $category->getImageUrl();

before$img = getCategoryImage($category);

function getCategoryImage($category) {
    $categoryCollection = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->getCollection()->addAttributeToSelect('image')->addIdFilter($category->getId());

    foreach($categoryCollection as $category) {
        return $category->getImageUrl();
    }
}

      

(now defunct) Link: http://www.magentocommerce.com/boards/viewthread/5026/P45/

+2


source







All Articles