Magento – check if category has image

magento-1.9

I am using the following code to list sub categories of main categories instead of products. It works great but I need to check if the category has an image. if it doesn't I want it to not show a broken image.

here is the code i'm using:

<?php
$_category  = $this->getCurrentCategory(); 
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper     = Mage::helper('catalog/category');
?>

<ul>
<?php foreach ($collection as $cat):?>
        <?php if($_category->getIsActive()):?>
            <?php 
                 $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
                 $_img = $cur_category->getImageUrl();  
            ?>
            <li>
                <a href="<?php echo $helper->getCategoryUrl($cat);?>">
                     <img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
                     <cite><?php echo $cat->getName();?></cite>
                </a>
            </li>
        <?php endif?>
<?php endforeach;?>

Best Answer

Try the below code,

Inside <li>,update the code as

         <li>
           <a href="<?php echo $helper->getCategoryUrl($cat);?>">
            <?php if($_img){?>                   
                <img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
            <?php }?>
             <cite><?php echo $cat->getName();?></cite>
           </a>
         </li>