3

How to give Condition For Category Image ? If Category Image Has Not Exist Then Display Placeholder Image/Thumbnail.

How to Write If Else Condition For That?

Code

 <?php
    $category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
    $categories = $category->getCollection()
            ->addAttributeToSelect(array('name', 'thumbnail'))
            ->addAttributeToFilter('is_active', 1)
            ->addIdFilter($category->getChildren())
    ?>
    <ul class="subcategories">
        <?php foreach ($categories as $category): ?>
            <li>
                <a href="<?php echo $category->getUrl() ?>"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
                <span><?php echo $category->getName() ?></span></a>
            </li>   
        <?php endforeach; ?>
    </ul>

I M uses for :

<?php if($category->getThumbnail()): ?>
    <img id="slide-img-1" src="<?php echo $this->helper('catalog/image')->init($category, 'thumbnail')->resize(135, 135); ?>" /></a>            
<?php endif; ?>

1 Answer 1

3
<?php if($category->getThumbnail()): ?>
    <img id="slide-img-1" src="<?php echo $this->helper('catalog/image')->init($category, 'thumbnail')->resize(135, 135); ?>" /></a>     
   <?php else: ?>
          <img id="slide-img-1" src="<?php echo Mage::getSingleton('catalog/product_media_config')->getBaseMediaUrl(). '/placeholder/' . placeholderUri ?>" /></a>  
<?php endif; ?>

You can then get the Magento placeholder URL by replacing placeholderUri above with one of the following, depending on preference:

Base Image: Mage::getStoreConfig('catalog/placeholder/image_placeholder')

Small Image: Mage::getStoreConfig('catalog/placeholder/small_image_placeholder')

Thumbnail: Mage::getStoreConfig('catalog/placeholder/thumbnail_placeholder');

3
  • Getting Error : Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\inetgp\app\design\frontend\inet\default\template\catalog\category\sub.phtml on line 27 Jan 18, 2017 at 8:10
  • 1
    @RushikeshSolanki I have update the answer please check
    – Arunendra
    Jan 18, 2017 at 8:34
  • 1
    @RushikeshSolanki is your problem solved using above answer? Jan 31, 2019 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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