问题
We've extended a homepage slider for a client so that they can feature products in this space.
As part of this, there are three image slots where we wish to get the products' main image and then two images from the media gallery (ideally randomly but not the end of the world if by ID).
To get a better understanding, please see screenshot of what we have so far:-

We're building the collection for this module using the following:-
$featured_products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->AddAttributeToFilter('featured', array('eq' => 1));
Getting the product's main image is no problem, this is working perfectly with the following:-
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(225); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />
And it is simple enough to make all three image slots use this main image as shown in the image above.
When we try to call getGalleryImages however, this always returns NULL
(example such as):-
<?php if (count($this->getGalleryImages()) > 0): ?>
<?php foreach ($this->getGalleryImages() as $_image): ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(100); ?>" width="100" height="100" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
<?php endforeach; ?>
<?php endif; ?>
Please could someone advise of the best approach to call the gallery images on the homepage. Is there something we can include to the collection build or would we need to add an observer.
Thanks in advance.
回答1:
Finally managed to get this working...
<?php $_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages(); ?>
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image) if ($i++ < 5) { $i++; ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
<?php } ?>
We included an if
statement in the foreach
loop to ensure we were only returning a maximum of 3 of the product's media gallery images.
End result, compared to original image, looking like:-

回答2:
It looks like you're calling getGalleryImages()
directly on the block. Call it on the product object (e.g., $product->getGalleryImages()
instead of $this->getGalleryImages()
).
来源:https://stackoverflow.com/questions/17812658/getgalleryimages-returning-null-in-magento-homepage