Magento provides facilities to get the Image URL from a category by using the catalog/category model.
1 |
echo Mage::getModel('catalog/category')->load($catId)->getImageUrl(); |
However, by default, it doesn’t provide the facility to get the thumbnail category image. You’d expect to be able to use getThumbnailUrl()
, but, you can’t.
In order to enable this functionality, you need to make some modifications to Magento.
The file you need to edit is /app/code/core/Mage/Catalog/Model/Category.php
. Due to the nature of Magento, you’d be best copying this to /app/code/local/Mage/Catalog/Model/Category.php
before amending the file. Once copied, you can edit this new file.
Around line 481, you’ll find the getImageUrl
function. Below this, we’re going to add a new function. This function is slightly modified from others I’ve seen, where you can pass it a parameter to get just the file name, or get the full path if you’d prefer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/** * Retrieve thumbnail image URL * * @return string */ public function getThumbnailUrl($fullpath = false) { $url = false; if ($image = $this->getThumbnail()) { if ($fullpath == true) { $url = Mage::getBaseUrl('media').'catalog/category/'.$image; } else { $url = $image; } } return $url; } |
Image Credit: Back, and to the left
Comment or tweet @douglasradburn