【问题标题】:getGalleryImages returning NULL in Magento homepage在 Magento 主页中返回 NULL 的 getGalleryImages
【发布时间】:2013-07-22 16:02:44
【问题描述】:

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

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

为了更好地理解,请看我们目前的截图:-

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

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

获取产品的主图像没问题,这与以下完美配合:-

<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) ?>" />

如上图所示,让所有三个图像槽都使用这个主图像非常简单。

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

<?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; ?>

请有人建议在主页上调用画廊图像的最佳方法。我们可以在集合构建中包含一些东西还是需要添加一个观察者。

提前致谢。

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    终于搞定了……

    <?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 张媒体库图片。

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

    【讨论】:

      【解决方案2】:

      您似乎是直接在块上调用getGalleryImages()。在产品对象上调用它(例如,$product-&gt;getGalleryImages() 而不是 $this-&gt;getGalleryImages())。

      【讨论】:

      • 这似乎仍然返回NULL。如有必要,我可以提供更多正在使用的代码。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      相关资源
      最近更新 更多