Magento 2 – Get Gallery Images

gallery-imagemagento2object

I'm trying to pull through the gallery images on the product page as I'm re doing the gallery. The code I'm using is:

<?php $_product = $block->getProduct(); ?>
<?php $gallery = $_product->getMediaGalleryImages(); ?>

<?php foreach ($gallery as $galleries): ?>
  <figure><?php echo $galleries ?></figure>
<?php endforeach; ?>

But this throughs back an error of "Object can't be converted to string"

Any ideas on how to fix this?

Best Answer

override the below template for getting gallery images url in product detail page

app/design/frontend/Vendor/ThemeName/Magento_Catalog/templates/product/view/gallery.phtml

<?php /* @escapeNotVerified */ $json=$block->getGalleryImagesJson();
$array = json_decode($json,true);
foreach($array as $item)
{
    $imgurl = $item['full'];
    echo $imgurl;        
}

use this above method I rendered full image. you can able to get full,small,thumbnail images. If you want to know about what are the images type available, just var_dump($json).

Related Topic