【问题标题】:Get all product images in Magento 2 on product list page在产品列表页面上获取 Magento 2 中的所有产品图片
【发布时间】:2016-04-24 15:11:48
【问题描述】:
在 Magento 1 中我一直使用
$_product->getMediaGallery('images')
但是在 Magento 2 的源代码中我看到了
$productImage = $block->getImage($_product, $image);
echo $productImage->toHtml();
它只获取第一张产品图片。
如何获得第二张或第三张图片(不仅仅是基础图片)?
GetMediaGallery 函数不存在?
【问题讨论】:
标签:
image
magento
media
product
mage
【解决方案1】:
第 1 步:从 Your Theme\Magento_Catalog\templates\product 打开 list.phtml
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
在您的 phtml 文件中添加上述内容
然后在您想要图库图片的产品循环中添加以下代码
<div class="left-carousel">
<div class="product-small-thumbs">
<?php $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());
$images = $product->getMediaGalleryImages();
if($images->count()>0){?>
<div class="carousel carousel-<?php $_product->getId()?>">
<?php
$i = 0;
foreach($images as $child){
$i++;
$productImage = $_imagehelper->init($product, 'product_page_image_large')
->setImageFile($child->getFile())->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(TRUE)->resize(81,53)
->getUrl();
$productImagedata = $_imagehelper->init($product, 'product_page_image_large')
->setImageFile($child->getFile())->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(TRUE)->resize(285,240)
->getUrl();
if($i==1) continue;
?>
<div class="slide">
<img data-id="smallthumbs-<?php echo $_product->getId();?>" data-img="<?php echo $productImagedata; ?>" src="<?php echo $productImage; ?>"/>
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
</div>