Magento Category image in Header

Magento Category image in Header

magento category image

Magento Category Image code does not work in header, here is a sample of default code you will want to use in header that doesnt work.

helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '

'.$this->htmlEscape($_category->getName()).'

';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>



Solution for Magento Category Image

After 2 3 hours of research and trial and error found this solution that works perfectly for Magento Category Image to work in header of my template. Don’t need to update xml or make any other change in the files, just copy this code anywhere in your website and it will work


helper('catalog/output');
$_category = Mage::getModel('catalog/layer')->getCurrentCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '

'.$this->htmlEscape($_category->getName()).'

';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>



The main difference in the default code and this solution is this
$_category = $this->getCurrentCategory();
while my solution for Magento Category Image uses
$_category = Mage::getModel('catalog/layer')->getCurrentCategory();

Hope it will work for you too. For any comments or feedback please leave me a message below.

NOTE: I am assuming you have basic understanding of Magento and know what is product and product categories in Magento. I also believe you know how to customize or add your own custom Magento template..