Simple Call to Get Category Images & Display in List

Simple Call to Get Category Images & Display in List

August 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Simple Call to Get Category Images & Display in List

Displaying Category Images Alongside Subcategories

In your current implementation, you’re loading subcategory data correctly for the given parent category ID (6). However, there are two main issues causing the images not to display:

  1. Incorrect Image URL Retrieval: The line $img = $category->getImageUrl(); is retrieving the image URL incorrectly. It should be within the foreach loop to ensure it corresponds with each specific category.

  2. Missing Category Image: Some categories may not have an associated image, causing the empty img src attribute in your output.

Let’s fix these issues:

<div id="menu_brands">
    <div class="brand_head">
        <h3><?php echo $this->__('Browse By Brand') ?></h3>
    </div>
    <div class="brand_list">
        <?php
            $cats = Mage::getModel('catalog/category')->load(6)->getChildren();
            $catIds = explode(',', $cats);

            foreach($catIds as $catId) {
                $category = Mage::getModel('catalog/category')->load($catId);
                $url = $category->getUrl();

                // Retrieve category image URL
                $imgUrl = $category->getImageUrl();
                if (!$imgUrl || empty($imgUrl)) {  // Add this line to handle categories without images
                    continue;
                }

                echo '<li>';
                echo '<a href="' . $url . '" title="' . $category->getName() . '">';
                echo '<img src="' . $imgUrl . '" width="auto" alt="' . $category->getName() . '" />';
                echo '</a>';
                echo '</li>';
            }
        ?>
    </div>
</div>

Key Changes Made:

  1. Moved the image retrieval line inside the foreach loop to associate it with each category.
  2. Added a check for empty or missing images (!$imgUrl || empty($imgUrl)). If an image is not available, the script will skip that iteration using continue.

Displaying Images in a Grid Format

For displaying the images in a grid format (3 or 4 per line), you can use CSS to achieve this:

.brand_list {
    display: flex;
    flex-wrap: wrap;
}

.brand_list li {
    flex-basis: calc(25% - 10px); /* Adjust based on desired column count and padding */
    margin-right: 10px;
    margin-bottom: 10px;
}

This CSS uses Flexbox to create a responsive grid layout for the category images. The flex-basis value determines the width of each item, while margin-right and margin-bottom add space between items.

Remember to enclose your PHP code within <?php ?> tags as shown in the example above. This ensures proper syntax highlighting and prevents issues with short opening/closing PHP tags (<? ?>).

With these changes, you should now be able to display category images alongside their subcategories effectively.

Catalog Errors

Our network