If category has image then show if not don't
-
11-10-2020 - |
Domanda
I want my categories to have the image above the content and the left column, whilst using 2columns-left.phtml and don't want an image placeholder showing if there's no image. So I need an if statement but not sure how to go about it!
Here's my code so far
<?php $categoryImage = Mage::getModel('catalog/layer')->getCurrentCategory()->getImage(); //Get the file name of the Image stored for the category ?>
<img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$categoryImage ?>" />
If anyone could help that would be much appreciated.
Soluzione
You can just add an if statement to check for $categoryImage
value.
Here's a better way to do it (basically you let Magento create the full URL)
$helper = $this->helper('catalog/output');
$category = Mage::getModel('catalog/layer')->getCurrentCategory()
if ($imgUrl = $category->getImageUrl()) {
$imgHtml = '<img src="'.$imgUrl.'" alt="' . $this->htmlEscape($category->getName()) . '" title="' . $this->htmlEscape($category->getName()) . '" />';
echo $helper->categoryAttribute($category, $imgHtml, 'image');
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange