I currently have a extension that sets a hover on an image when ever it finds a label that is called 'hover'. I wish to change this to hover an image if there is an image with sort order id which is '2'. But I do not know how to do this. The code is below.

    $html = "";
    $imgcount = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages()->count();
    if ($imgcount>0):
        $_gallery = Mage::getModel('catalog/product') -> load($_product -> getId()) -> getMediaGalleryImages();
        foreach ($_gallery as $_image ):
            if ($_image->getLabel() == 'hover'):
                $html = '<span class="hover-image"><img ' . $this->getImgSources($_product, $imgType, $w, $h, $_image -> getFile()) . ' alt="" /></span>';
    return $html;

I was hoping it would be as simple as if ($_image->getSortOrder() == 2): but that was just flawed thinking. Any ideas?

  • Is this from media.phtml? – Renon Stewart Oct 7 '14 at 16:58
  • Try getDefaultPosition() Instead of sortorder() – Amit Bera Oct 7 '14 at 17:00
  • It's for images.php and I will give default position a shot thanks. – Francis Oct 8 '14 at 7:50

Try $_image->getPositionDefault()


    [value_id] =&gt; 2
    [file] => /l/a/label.jpg
    [label] => Label
    [position] => 0
    [disabled] => 0
    [label_default] => Label
    [position_default] => 0
    [disabled_default] => 0
    [url] => http://www.site.com/media/catalog/product/l/a/label.jpg
    [id] => 2
    [path] => /var/www/site/media/catalog/product/l/a/label.jpg
  • Thank you for that. That worked on a test image, but I accidently came across getPosition and it works wonders without me having to change much. – Francis Oct 8 '14 at 8:08

R.S' answer works but I came across a simpler way for me to do it. I simple changed.

if ($_image->getLabel() == 'hover'):


if ($_image->getPosition() == 2):

Thanks for your help folks, wouldn't of came across this otherwise. I know it looks stupid, but in a panic I missed the most obvious thing in the world and that was the fix.

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.