Magento主页中的getGalleryImages返回NULL [英] getGalleryImages returning NULL in Magento homepage

查看:114
本文介绍了Magento主页中的getGalleryImages返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经为客户扩展了首页滑块,以便他们可以在此空间中展示产品.

We've extended a homepage slider for a client so that they can feature products in this space.

其中一部分是我们希望获得产品主图像的三个图像槽,然后是媒体库中的两个图像(理想情况下是随机的,但如果按ID则不是世界末日).

As part of this, there are three image slots where we wish to get the products' main image and then two images from the media gallery (ideally randomly but not the end of the world if by ID).

要获得更好的理解,请查看到目前为止的屏幕截图:-

To get a better understanding, please see screenshot of what we have so far:-

我们正在使用以下方法为此模块构建集合:-

We're building the collection for this module using the following:-

$featured_products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->AddAttributeToFilter('featured', array('eq' => 1));

获取产品的主图像是没有问题的,它可以完美地与以下各项配合使用:-

Getting the product's main image is no problem, this is working perfectly with the following:-

<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(225); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />

很容易使所有三个图像插槽都使用该主图像,如上图所示.

And it is simple enough to make all three image slots use this main image as shown in the image above.

但是,当我们尝试调用getGalleryImages时,它总是返回NULL(例如示例):-

When we try to call getGalleryImages however, this always returns NULL (example such as):-

<?php if (count($this->getGalleryImages()) > 0): ?>
<?php foreach ($this->getGalleryImages() as $_image): ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(100); ?>" width="100" height="100" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
<?php endforeach; ?>
<?php endif; ?>

请有人建议最好的方法来调用主页上的图库图像.有什么可以包含在集合构建中的东西吗?还是需要添加一个观察者?

Please could someone advise of the best approach to call the gallery images on the homepage. Is there something we can include to the collection build or would we need to add an observer.

谢谢.

推荐答案

最终设法使它正常工作...

Finally managed to get this working...

<?php $_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages(); ?>    
<?php if($_images){?>            
    <?php $i=0; foreach($_images as $_image) if ($i++ < 5) { $i++; ?>
        <img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
    <?php } ?>
<?php } ?>

我们在foreach循环中添加了if语句,以确保我们最多只返回该产品的3个媒体库图像.

We included an if statement in the foreach loop to ensure we were only returning a maximum of 3 of the product's media gallery images.

与原始图像相比,最终结果看起来像:-

End result, compared to original image, looking like:-

这篇关于Magento主页中的getGalleryImages返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆