This video-tutorial shows you how to inherit catalog images from a parent-category in subcategories of Magento. Instead of having to give each subcategory its own image, you can save yourself time by just reusing the parent-category's image if the current category has no image of its own.

{youtube ZXwwvmjMKNE}

Below you can find the sample code used in this video-tutorial. It should be placed in your copy of app/design/frontend/enterprise/template/catalog/category/view.phtml.

<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';
$_imgUrl = $_category->getImageUrl();

if (!$_imgUrl) {
    foreach (array_reverse($_category->getParentIds()) as $id) {
        if ($_imgUrl = Mage::getModel('catalog/category')->load($id)->getImageUrl()) {
            break;  
        }
    }
}

if ($_imgUrl) {
    $_imgHtml = '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" class="category-image" />';
    $_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>
Posted on November 8, 2009

About the author

Author Jisse Reitsma

Jisse Reitsma is the founder of Yireo, extension developer, developer trainer and 3x Magento Master. His passion is for technology and open source. And he loves talking as well.

Sponsor Yireo

Upcoming events

Oct
28
Oct
31
LIEF Amsterdam
Nov
08

Looking for a training in-house?

Let's get to it!

We schrijven niet te commerciële dingen, we richten ons op de technologie (waar we dol op zijn) en we komen regelmatig met innovatieve oplossingen. Via onze nieuwsbrief kun je op de hoogte blijven van al deze coolness. Inschrijven kost maar een paar seconden.

Do not miss out on what we say

This will be the most interesting spam you have ever read

We schrijven niet te commerciële dingen, we richten ons op de technologie (waar we dol op zijn) en we komen regelmatig met innovatieve oplossingen. Via onze nieuwsbrief kun je op de hoogte blijven van al deze coolness. Inschrijven kost maar een paar seconden.