简体   繁体   中英

Magento: How to get an image by label?

I have another question for you guys :P

On the product grid view i want to change the image when you hover over it with your mouse. The image i want to switch it too should have a labelname of 'back'.

I almost have it..:P

When i use this code in media.phtml:

<?php $imageByLabel = $_product->getMediaGalleryImages()->getItemByColumnValue('label', 'back');?>
<?php echo $this->helper('catalog/image')->init($_product, 'image', $label->getFile())->resize(235, 350);?>

It returns something like this: http://www.mywebsite.com/product/images/image1 When i use this in the source attribute of my image it displays the correct image.

But when i use this code in the catalog/product/list.phtml i get the following error:

Fatal error: Call to a member function getItemByColumnValue() on a non-object in C:\\Development\\FabriqMagento\\app\\design\\frontend\\default\\fabriq\\template\\catalog\\product\\list.phtml on line 95

PS: line 95 is the following line:

<?php $imageByLabel = $_product->getMediaGalleryImages()->getItemByColumnValue('label', 'back');?>

Any help is appreciated:)

If you review your code, $imageByLabel is not doing anything so it is not needed.

Try:

<?php echo Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages()->getItemByColumnValue('label','black')->getFile();  ?>

from list.phtml

For not list.phtml:

$_item = $this->getItem();
$_product = $_item->getProduct();

@Francis Kim - method - also with resiz

$_image = Mage::getModel('catalog/product')->load( $_product->getId() )->getMediaGalleryImages()->getItemByColumnValue('label','FRONTSHOP');
echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(75);

Only issue that i found is that its not working with Exclude value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM