molleman
molleman

Reputation: 2946

How to get access to custom Image Attribute in list.phtml in Magento

I am creating a website in magento.

So i have added a new media image attribute to my products. I want to use a specific image for my homepage but a few products will not have this image.

So i have created a new list.phtml file called list-homepage.phtml.

so i am doing this at the moment within product collection for loop in list.phtml.

$product=Mage::getModel('catalog/product')->load($_product->getId());

i am loading the product again from above, and i test to see if this product has the new image attribute

<?php if ($product->getMaleFemaleImage()){  ?>
      <img src="<?php echo $this->helper('catalog/image')->init($product, 'male_female_image')->setQuality(100)->resize($imgSize); ?>" width="<?php echo $imgSize ?>" height="<?php echo $imgSize ?>" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'male_female_image'), null, true) ?> " />
<?php }else{ ?>
     <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->setQuality(100)->resize($imgSize); ?>" width="<?php echo $imgSize ?>" height="<?php echo $imgSize ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<?php }?>

This works BUT As you can see from above i have to load the product again and i do not want to do this. Is there any way to get access to the male_female_image attribute i have created.

Upvotes: 0

Views: 5877

Answers (1)

MagePal Extensions
MagePal Extensions

Reputation: 17656

Take a look @ Magento - add media gallery attribute to product flat table

Open app/design/frontend/default/yourtheme/layout/catalog.xml

In <catalog_category_layered translate="label"> and <catalog_category_default translate="label"> find the line below

 <block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
 <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
 <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">

and (add <action method="addAttribute">....)

<block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
add -->   <action method="addAttribute"><name>MaleFemaleImage</name></action>
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">

Read more @

Upvotes: 4

Related Questions