在产品列表页面上获取Magento 2中的所有产品图像



在Magento 1中,我一直使用

$_product->getMediaGallery('images')

但是在Magento 2的来源中,我看到

$productImage = $block->getImage($_product, $image);
echo $productImage->toHtml();

它只获得了第一张产品图片。如何获取第二张或第三张图像(不仅是基本图像)?

GetMediaGallery 函数不存在?

第 1 步:从您的主题\Magento_Catalog\模板\产品中打开 list.phtml

$objectManager =  MagentoFrameworkAppObjectManager::getInstance();

$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');

在您的 phtml 文件中添加上面

然后在产品循环中添加以下代码,您需要图库图像

<div class="left-carousel">
                   <div  class="product-small-thumbs">
                    <?php $product = $objectManager->create('MagentoCatalogModelProduct')->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>

最新更新