I'm trying to pull through the gallery images on the product page because customizing the gallery with code:

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

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

But first image is not loading. How fix this?


Try below code to get Array of gallery images in gallery.phtml file

| improve this answer | |

You can try in this way (with objectmanager)

    $_product = $block->getProduct();
    $objectmanager = \Magento\Framework\App\ObjectManager::getInstance();
    $product_id = $_product->getId();
    $productimages = array();
    $product = $objectmanager->create('Magento\Catalog\Model\Product')->load($product_id);
    $productimages = $product->getMediaGalleryImages();
    foreach ($productimages as $productimage) {

        echo "<img src=" . $productimage['url'] . " alt=" . $_product->getName() . ">";

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.