6

I want to display image 2 from a product in the product view page next to the description. I need some help putting this code together.

I found some code online, but it gives me image 1 (base image). I need the second image displayed.

Ho can I do this?

1
  • edit your post and join the code you've found, and tell us what you've tried so far :) Jun 3 '15 at 13:22
2

Goto admin panel, Catalog >> Manage Products, select your product and goto images section. Use Sort Order column to get the second image.

Please paste the below code in media.phtml file of your theme.

     <?php $i=0; foreach ($this->getGalleryImages() as $_image): ?>
          <?php if($_image['position_default'] == 2): ?>
          <img id="image-<?php echo $i; ?>"
               class="gallery-image"
               src="<?php echo $this->getGalleryImageUrl($_image); ?>"
               data-zoom-image="<?php echo $this->getGalleryImageUrl($_image); ?>" />
          <?php endif;?>
    <?php $i++; endforeach; ?>

Note: Place the image at second order which you want to display at front-end.

Let us know incase of any query/concern.

1

Try something like this:

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

foreach ($product_attr->getMediaGalleryImages() as $image) {
    echo $image->getUrl();
}
1

The "second" image, do you mean the second you have uploaded? Or the second in the order you have set? I think the best way to achieve this, would be to set a label to your image (you can do that in the Images tab when you edit the product).

Then you can just load it this way:

<?php 
    $product = Mage::registry('current_product'); // product is already loaded, no need to load it again)
    $myImageLabel = 'secondimage-label';
    $imageUrl = $product->getMediaGalleryImages()->getItemByColumnValue('label', $myImageLabel)->getFile();  
?>
0

The easiest way is to create a new attribute in magento and set it as media attribute. Once you have done that, you will see it in the product image tab.

Let's say the attribute you created is called text-logo and to show it on the front end you can use the following code:

  img src="getBaseUrl()."media/catalog/product".$_product['text_logo']?>" />
1
  • Creating a new image attribute (same as "base_image" and "thumbnail") is the right way to go. But you should use the same code to render it as you would with the base image: src="<?php echo $this->helper('catalog/image')->init($_product, 'text_logo') ?>" Jun 25 '15 at 9:28

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.