Question

$category = Mage::getModel('catalog/category')->load('2')->getChildrenCategories();
foreach ($category as $cat){

    $entity_id = $cat->getId();
    $name = $cat->getName();
    $url_key = $cat->getUrlKey();
    $url_path = $cat->getUrlPath();
    $skin_url = $cat->getImageUrl(); 


 <img src="<?php echo $skin_url; ?>" />

above mentioned code is not working for me, if any one have to the point solution, then please help.

Was it helpful?

Solution 3

This is the Solution

$category =  Mage::getModel('catalog/category')->getCollection()
                            ->addAttributeToSelect('*')
                            ->addAttributeToFilter('parent_id', 2);
    foreach ($category as $cat){
            $entity_id = $cat->getId();
            $name = $cat->getName();
            $url_key = $cat->getUrlKey();
            $url_path = $cat->getUrlPath();
            $skin_url = $cat->getThumbnail(); 
    <img src="<?php echo Mage::getBaseUrl('media')?>catalog/category/<?php echo $skin_url; ?>" />
    } #endforeach
?>

OTHER TIPS

Try it like this:

$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('parent_id', 2);

foreach ($categories as $cat) {
    $entity_id = $cat->getId();
    $name = $cat->getName();
    $url_key = $cat->getUrlKey();
    $url_path = $cat->getUrlPath();
    $skin_url = $cat->getImageUrl(); 
    echo '<img src="'.$skin_url.'" />';
}
function getImageUrl($category)
 {

$cur_category=Mage::getModel('catalog/category')->load($category->getId());

$layer = Mage::getSingleton('catalog/layer');

$layer->setCurrentCategory($cur_category);
$url = $this->getCurrentCategory()->getImageUrl();
return $url;
};   

 if ($image = $cat->getImageUrl()) {
                <img src="<?php echo $_imgUrl ?>"/ >
    }

Or

<?php if($cat->getThumbnail()): ?>

<img src="<?php echo Mage::getBaseUrl('media')?>catalog/category/<?php echo $cat->getThumbnail() ?>

<?php endif; ?>

More info

There are 2 reasons your code is not working.

First of all, there are syntax errors. Maybe you've just given us two snippets, but if that's not the case then you have php tags inside php code, and you've omitted the closing bracket of the foreach statement.

Secondly, getChildrenCategories() does not load all category data, just the very basics. So getImageUrl() returns nothing because image data was not loaded.

The funny thing is that Marius' answer solves both of these problems, yet you say it still won't work. In which case perhaps you need to refresh you cache and/or indexes.

System > Cache Management > Additional Cache Management > Flush Catalog Images Cache
System > Index Management > Category Flat Data

Also, I gather this code is inside a template, in which case you might have to refresh the blocks html cache too:

System > Cache Management > Blocks HTML output

Combined with Marius' code, this should do the trick.

PS. If the code isn't inside a template then that opens up a whole load more possible explanations, e.g. Blocks and Models should most probably be using return instead of echo.

** EDIT **

PPS. The title of your question says "thumbnail", rather than "image". While categories do have thumbnails, the Magento team have for some reason neglected to write a getThumbnailUrl() function to complement getImageUrl() - at least this is true in CE 1.7 and 1.8, haven't checked 1.9 yet. So if it really is the thumbnail that you want, copy Mage_Catalog_Model_Category::getImageUrl() and modify accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top