I want to use the first and the second image of a product, I'm now able to use the first one. But how can I reach the second image?

Does the $_product->getMediaGallery('images') exists in Magento 2? If not how can I reach the same goal?

I tried to use this:

foreach ($product->getMediaGalleryImages() as $product_img) :
    echo $product_img;

And i recived the following error "Error filtering template: Warning: Invalid argument supplied for foreach() in /var/www/html/magento2/app/design/frontend/XXX/xxx/Magento_T‌​heme/templates/xxxx.‌​phtml on line 27"

  • You can use $_product->getMediaGalleryImages(); to get media images in Magento 2 – Jaimin Sutariya Mar 3 '17 at 18:32
  • Please add your code to your question to check the error. – Jaimin Sutariya Mar 6 '17 at 9:09
  • @JaiminSutariya there it is – RuiVBoas Mar 6 '17 at 10:21
  • Please add line where you are instantiating $product in your code. Are you loading it using Product factory? – Jaimin Sutariya Mar 6 '17 at 10:48
  • @JaiminSutariya , I just create a block for that and I get each product from $block->getProductCollection() – RuiVBoas Mar 6 '17 at 12:05

There is a getMediaGalleryImages function in the Magento\Catalog\Model\Product class which will return the media gallery images.

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.