2
$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.

4

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.'" />';
}
  • lol, your solution not working today – Muhammad Shoaib Murtaza Oct 2 '14 at 10:29
  • By not working you mean...? – Marius Oct 2 '14 at 10:31
  • name of subcategories already displayed, but image/thumbnail was not displaying. and same is now... – Muhammad Shoaib Murtaza Oct 2 '14 at 10:33
  • Are your sure you have the image field filled in for the categories? – Marius Oct 2 '14 at 10:36
  • it should be getThumbnail() instead of getImageUrl(), however thanks for your help, your solution helped half way – Muhammad Shoaib Murtaza Oct 2 '14 at 14:20
2
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

2

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
?>
1

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.

  • You mentioned " the Magento team have for some reason neglected to write a getThumbnailUrl() function to complement getImageUrl()" But i have got my imges using getThumbnail(). – Muhammad Shoaib Murtaza Oct 2 '14 at 14:18
  • Yep that'll work too. Just as you can use <?php echo Mage::getBaseUrl('media')?>catalog/category/<?php echo $cat->getImage(); ?> instead of <?php echo $cat->getImageUrl(); ?>, so you can use <?php echo Mage::getBaseUrl('media')?>catalog/category/<?php echo $cat->getThumbnail(); ?> instead of the (non-existant) <?php echo $cat->getThumbnailUrl(); ?>. Personally, I prefer to add the getThumbnailUrl function to the model, then I can use the shorter syntax in my templates. – Doug McLean Oct 2 '14 at 14:58
  • By the way, your solution (which I can't comment on as I currently have < 50 reputation) still has php tags inside php code, which will not work. That HTML that you've got inside your foreach loop needs to break out of the php code, like so: ?><img src="<?php echo Mage::getBaseUrl('media')?>catalog/category/<?php echo $skin_url; ?>" /><?php – Doug McLean Oct 2 '14 at 15:02
  • yes, code may be written incorrect during comments, but my focus was only at methods which can fulfill my requirement. – Muhammad Shoaib Murtaza Oct 2 '14 at 15:10
  • <?php echo $cat->getImage(); ?> not working, just getThumbnail() method is working in my situation. – Muhammad Shoaib Murtaza Oct 2 '14 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.