0

This is the gallery.phtml code which is displaying main thumbnail image only

    <?php
$images = $block->getGalleryImages()->getItems();
$mainImage = current(array_filter($images, function ($img) use ($block) {
    return $block->isMainImage($img);
}));

if (!empty($images) && empty($mainImage)) {
    $mainImage = $block->getGalleryImages()->getFirstItem();
}

$helper = $block->getData('imageHelper');
$mainImageData = $mainImage ?
    $mainImage->getData('medium_image_url') :
    $helper->getDefaultPlaceholderUrl('image');

?>

<div class="gallery-placeholder _block-content-loading" data-gallery-role="gallery-placeholder">
    <img
        alt="main product photo"
        class="gallery-placeholder__image"
        src="<?= /* @noEscape */ $mainImageData ?>"
    />
</div>

<script type="text/x-magento-init">
    {
        "[data-gallery-role=gallery-placeholder]": {
            "mage/gallery/gallery": {
                "mixins":["magnifier/magnify"],
                "magnifierOpts": <?= /* @noEscape */ $block->getMagnifier() ?>,
                "data": <?= /* @noEscape */ $block->getGalleryImagesJson() ?>,
                "options": <?= /* @noEscape */ $block->getGalleryOptions()->getOptionsJson() ?>,
                "fullscreen": <?= /* @noEscape */ $block->getGalleryOptions()->getFSOptionsJson() ?>,
                 "breakpoints": <?= /* @noEscape */ $block->getBreakpoints() ?>
            }
        }
    }
</script>

Any solution??????

I want other images also be displayed not only main thumbnail image

0
  1. create custom block file :

app\code\Vendor\Extension\Block\Myblock.php

<?php 

namespace Vendor\Extension\Block;

use Magento\Framework\View\Element\Template;

class Myblock extends Template
{
    protected $productloader; 

    public function __construct(\Magento\Catalog\Model\ProductFactory $productloader)
    {
        $this->productloader = $productloader;
    }

    public function getProductAllImages($productId)
    {
       $product = $this->productloader->create()->load($id);
       return $product->getMediaGalleryImages();

    }

}
  1. Now on your phtml do code like this one.

    $product_id = 32;
    $productimages = $block->getProductAllImages($product_id);
    foreach($productimages as $productimage)
    {
       echo "<img src = ".$productimage['url']. " height=100 width=100 />";
    }
    
| improve this answer | |
  • But I don't want to set product id=32 – Anonymous Record Mar 6 at 10:39
  • I want it to get Product id of current product automatically – Anonymous Record Mar 6 at 10:39
  • That you want to get from the registry if you on product page. – Dhiren Vasoya Mar 6 at 10:45
  • yes from registry it should get automatically – Anonymous Record Mar 6 at 10:46
  • Then pass that Id and do change accordingly, so you get images for that product. – Dhiren Vasoya Mar 6 at 11:31

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.