Magento – Magento 2 get product gallery images on product listpage without load model in loop

gallery-imagemagento2product-list

I can get gallery images on product-list page using below code:

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());        
    $images = $product->getMediaGalleryImages();
    foreach($images as $child){ ?>
        <img src="<?php echo $child->getUrl(); ?>" >
<?php } ?>

I know load model in loop is bad practice and it effect to performance.Can anyone please suggest me best way to achieve same function.

Best Answer

=> Use this code. It maybe helpful to you.

<?php
$product->getMediaGalleryImages();

foreach($images as $child)
 { ?>

"<?php echo $child->getPath(); ?>
"<?php echo $child->getUrl(); ?>

}
Related Topic