Display different product images for categories in magento

Display different product images for categories in magento

October 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display different product images for categories in magento

Displaying Different Product Images for Categories in Magento

Hello! I’m excited to help you solve this challenge of displaying different product images based on categories in your Magento e-commerce store. Let’s dive into the solution step-by-step.

Understanding Your Requirements

You have a Magento website selling jumpers, with each jumper having up to three images:

  1. The base image without any model.
  2. An image featuring a male model wearing the jumper.
  3. An image featuring a female model wearing the jumper.

Some products might have all three images, while others may only have the base image and either the male or female image. On your homepage, you want to display the jumpers with no models.

Now, you’d like to create two categories: ‘Male’ and ‘Female’. When a user selects one of these categories (e.g., the ‘Male’ category), you want to show only the images featuring the relevant model in that page.

Solution Approach

To achieve this, we’ll follow a two-step approach:

  1. Configure Your Product Attributes
  2. Create and Configure Categories

Let me walk you through each step with relevant code examples and best practices.

Step 1: Configure Your Product Attributes

Currently, your images are likely configured as the standard product image attribute. To distinguish between them, let’s create new attributes for the male and female model images:

  • Go to Admin -> Catalog -> Attributes -> Manage Attributes
  • Click on “Add New Attribute”
  • Set the following properties:
    • Attribute Code: product_male_image (for the male model image)
    • Frontend Label: Male Model Image
    • Catalog Input Type for Store Owner: File
    • Default Value: Leave blank
  • Click on “Save Attribute”

Repeat these steps to create another attribute called product_female_image.

Step 2: Create and Configure Categories

Now, let’s set up your male and female categories:

Creating the Categories

  1. Go to Admin -> Catalog -> Categories
  2. Click on “Add Root Category”
  3. Enter the name ‘Male’ or ‘Female’
  4. Set the other properties as per your needs
  5. Click on “Save Category”

Filtering Products by Category

To display only the relevant images in each category, you’ll need to apply a filter when retrieving products:

// Assuming $categoryId is the ID of your male or female category
$filteredProducts = Mage::getModel('catalog/category')
    ->load($categoryId)
    ->getProductCollection()
    ->addAttributeToSelect(array(
        'product_male_image',
        'product_female_image'
    )));

This code retrieves all the products associated with your specified category and selects the male or female model image attributes.

Displaying Images Based on Category

In your category view template (usually located at app/design/frontend-your-theme/layout/catalog/category/view.phtml), you can now loop through your filtered products and display the relevant images:

<?php foreach ($filteredProducts as $product): ?>
  <?php
    $maleImage = $product->getData('product_male_image');
    $femaleImage = $product->getData('product_female_image');

    if ($categoryId == 'male_category_id') {
      echo Mage::helper('catalog/image')->init($product, 'product_male_image')->resize(300);
    } else if ($categoryId == 'female_category_id') {
      echo Mage::helper('catalog/image')->init($product, 'product_female_image')->resize(300);
    } else {
      // Display the base image
      echo $this->helper('catalog/image')->init($product, 'image');
    }
  ?>
<?php endforeach; ?>

Remember to replace 'male_category_id' and 'female_category_id' with your actual category IDs.

Summary

We’ve successfully configured the product attributes and categories to display different images based on the selected category. By following this approach, you can showcase jumpers featuring male models in the ‘Male’ category and jumpers featuring female models in the ‘Female’ category.

I hope this comprehensive guide has provided a clear solution to your challenge! If you need further assistance or have any questions about Magento development, feel free to reach out at LaravelCompany.com.

Thank you for reading, and I wish you all the best with your jumper e-commerce store!

Catalog Admin Configuration CLI

Our network