Some of my products have the first image set to position 0 and some has the first image set for position 1. Because of that, I need to run a check determining the id of the first gallery image. How can I do this?

I was thinking something like


But that doesnt seem to work..



As getMediaGalleryImages() is object of Varien_Data_Collection class ,so you does not get data using generic php array index [0].

In this case you need to use getFirstItem() function of Varien_Data_Collection.


echo $mediaGallery->getFirstItem();

For getting first item position you need use position_default instead of position

echo $mediaGallery->getFirstItem()->getPositionDefault();
|improve this answer|||||
  • Thanks. Managed to get it to work, but for some reason I always get sortOrder 1 for the following, even though some of them have position 0: $_gallery = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); $firstposition = $_gallery->getFirstItem()->getPosition(); – user2806026 Oct 8 '15 at 8:15
  • Would it be easier to get base image and call getposition on that? – user2806026 Oct 8 '15 at 8:23
  • echo $mediaGallery->getFirstItem()->getPositionDefault(); use this code first item position – Amit Bera Oct 8 '15 at 9:29
  • Still gives me 1 for all: $_gallery = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); $firstposition = $_gallery->getFirstItem()->getPositionDefault(); – user2806026 Oct 8 '15 at 9:53

To get an image from the gallery based on an attribute value, use getItemByColumnValue function. Also, to get the media gallery including excluded images, use getMediaGallery('images')


Always get the second image in your specific case

$images = $product->getMediaGallery('images');
$i = 0;
foreach ($images as $image)
if ($i == 1)
   $secondImage = $image;

First answer:

To get the first image position:

$_gallery = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages();

$_firstImagePosition = $_gallery->getFirstItem()->getPosition();

To get all the images position:

$_gallery = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages();

foreach ($_gallery as $_image)
$_imagePosition = $_image->getPosition();
|improve this answer|||||
  • Thanks. Can you also clarify why $_gallery = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImag‌​es(); $firstposition = $_gallery->getFirstItem()->getPosition(); always returns 1 even though a lot of my products have imges with position 0? – user2806026 Oct 8 '15 at 8:59
  • @Digital Pianism thanks .. i will cross check.. – Amit Bera Oct 8 '15 at 9:12
  • Actually seem like there isnt any index in the log: pastebin.com/015ThH3Z – user2806026 Oct 8 '15 at 9:17
  • @DigitalPianism ,you are right,Thanks for notice me the wrong ....Already give you upvote – Amit Bera Oct 8 '15 at 9:24
  • This still returns 1 for all images, even though some should be 0: ` $i_im = 0; foreach ($_gallery as $_image ): $i_im++; if($i_im == 1){ $_imagePosition = $_image->getPosition(); }` – user2806026 Oct 8 '15 at 9:49

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.