How to get all images of a specific product in Magento 2?
5 Answers
<?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 } ?>
-
-
-
$productid=12; $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productid); You want to give product id. You want to fetch products and pass product ids.– P RamuluDec 4, 2018 at 6:24
Your question is too broad. you didn't specify where or how do you want to get images. if you want to show all images in custom slider then try below code:
<ul id="image-gallery" class="gallery list-unstyled cS-hidden">
<?php $images = $product->getMediaGalleryImages(); ?>
<?php foreach ($images as $_image) : ?>
<li data-src="">
<a href=""><img alt="Rootways Extension" class="img-responsive" src="<?= $_image->getUrl();?>"></a>
</li>
<?php endforeach ?>
</ul>
You can get all images by below code to default gallery.phtml file
<?php
foreach($block->getGalleryImages() as $image){ ?>
<img src="<?php echo $image->getData('medium_image_url') ?>">
<?php }
?>
-
but I need only the images of specific product, not all images of all products– glafcheAug 17, 2018 at 12:40
Try this:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
$mediaPath = $directory->getPath('media');
$product_id = 10;
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
$galleryReadHandler = $objectManager->get('Magento\Catalog\Model\Product\Gallery\ReadHandler');
$galleryReadHandler->execute($product);
$images = $product->getMediaGalleryImages();
foreach($images as $image){
$temp = $mediaPath."/catalog/product".$image->getFile();
echo "<pre>"; print_r($temp);
}
Try below code. If you want to show product images category wise then use below code.
<?php $catid = $block->getCurrentCategory()->getId(); ?>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get('\Magento\Framework\App\State');
//$appState->setAreaCode('frontend');
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
if ($catid == 3)
{
$categoryId = $catid;
$category = $categoryFactory->create()->load($categoryId);
$categoryProducts = $category->getProductCollection()
->addAttributeToSelect('*');
?>
<div id="Coverflow">
<ul class="flip-items">
<?php foreach($categoryProducts as $product): ?>
<li>
<a href="<?php echo $product->getProductUrl() ?>">
<img src="<?php echo $mediaUrl.'catalog/product/'.$product->getImage() ?>" alt=""/>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php } ?>
I hope it will help you.
-
1
-
$product->getMediaGalleryEntries()
$product->getMediaGalleryEntries()
returns all files by->getFile()
saved in a product gallery, including hidden images.