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?

2 Answers 2


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


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() . ">";


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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