In my store when a product has more then one image, the (Product)page shows the images in a jQuery slideshow. The code for this slider is created from the thumbnails in media.phtml with the following snippet:

<?php if (count($this->getGalleryImages()) > 0): ?>
<div class="more-views">
    <ul id="productSlider">

    <?php $i=0; foreach ($this->getGalleryImages() as $_image): ?>
        if (($filterClass = $this->getGalleryFilterHelper()) && ($filterMethod = $this->getGalleryFilterMethod()) && !Mage::helper($filterClass)->$filterMethod($_product, $_image)):
            <a class="thumb-link" rel="media" href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(800); ?>" title="<?php echo $this->escapeHtml($_image->getLabel()) ?>" data-image-index="<?php echo $i; ?>">
                <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(265); ?>"
                     width="265" height="265" alt="<?php echo $this->escapeHtml($_image->getLabel()) ?>" />
    <?php $i++; endforeach; ?>
<?php endif; ?>


The thumbnails are presented in reverse order, resulting in the main image always being displayed last (and out of sight).


What would I need to change to reverse the order in the thumbnail generation so the "main" image gets displayed first?


3 Answers 3


if $this->getGalleryImages() returns a collection you can just transform it to an array, reverse it and iterated it.

$images = array();
foreach ($this->getGalleryImages() as $image) {
    $images[] = $image;
$images = array_reverse($images);

foreach ($images as $image) {
.....//your display here

Well you could try to change the php for loop to be

<?php $i=0; foreach(array_reverse($this->getGalleryImages()) as $_image): ?>


<?php $i=0; foreach(array_reverse($this->getGalleryImages()->getData()) as $_image): ?>

if getGalleryImages() returns a collection

but that would be kind of a hack. That may meet your immediate needs however. In general, you should post the code for getGalleryImages() so we can see if the problem lies there or in possibly in your DB.

  • Pretty sure getGalleryImages will return a collection, not an array so this probably won't work. Commented Mar 27, 2015 at 16:52
  • If it returns a collection, you can use array_reverse($collection->toArray()) Commented Jul 28, 2015 at 21:56

Well you must to modily the loop of php array:

<?php $i=0; foreach(array_reverse($this->getGalleryImages()->getData()) as $_image): ?>

Good Job

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.