Being robust & feature pack CMS, Magento is serving tons of options as a part of the community edition to fulfill the needs of a store owner. Products and services sold within the store are assets and identity of the organization. Selling these stuff online, the product images, videos, docs and descriptions that are given on product page is helpful for the store owner to sell their product and for a customer is a key to find best product or services by comparing to thousands of other stores. In life rush, no one has time to read everything in detailed at that time showcasing products with different images will not only save time but also increases a chance to impress a customer visually.
Sometimes, we need to display these product images at somewhere else in the store at that time identifying product URLs or to re-uploading same product images may take more time and space. Today at MageComp blog, we are back with a piece of code that will help you to grab product image URLs with the help of object manager in Magento 2.
Here is quick code to show specific product images with the sidebar.
$objectmanager = \Magento\Framework\App\ObjectManager::getInstance();
$product_id = 32; //Replace with your product ID
$productimages = array();
$product = $objectmanager ->create('Magento\Catalog\Model\Product')->load($product_id);
$productimages = $product->getMediaGalleryImages();
foreach($productimages as $productimage)
echo "<img src = ".$productimage['url']. " height=100 width=100 />";
Moreover, you can use this same code any places in Magento like Block, Model file, helper file or in phtml file as per need.
Lastly, Comment down below if you face any issue while using this code.