1

Situation:

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): ?>
        <?php
        if (($filterClass = $this->getGalleryFilterHelper()) && ($filterMethod = $this->getGalleryFilterMethod()) && !Mage::helper($filterClass)->$filterMethod($_product, $_image)):
            continue;
        endif;
        ?>
        <li>
            <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()) ?>" />
            </a>
        </li>
    <?php $i++; endforeach; ?>
    </ul>
</div>
<?php endif; ?>

Problem:

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

Question:

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

1

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
}
0

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

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

or

<?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. – Peter O'Callaghan Mar 27 '15 at 16:52
  • If it returns a collection, you can use array_reverse($collection->toArray()) – simonthesorcerer Jul 28 '15 at 21:56
0

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, privacy policy and cookie policy

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