在 Magento 中获取包含标签的媒体库图像列表

Get a list of media gallery images that contain label in Magento

我创建了一个可配置的产品,并添加了一些带有不同标签的图像。我如何才能在 media.phtml 文件的前端仅包含包含标签的图像:mylabel 以及当我按下此图像以获得更多视图等功能时?我有代码,但不知道为什么只显示第一张图片,而不是全部。

$prodimg = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages()->getItemByColumnValue('label','mylabel');
if ($prodimg != '') {
    echo "<img src='" .$this->helper('catalog/image')->init($_product, 'image', $prodimg->getFile()). "' class='img-responsive' />";
}

我还没有测试过这个,但我的想法是分解你的代码示例:

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

这很好,尽管只有当您需要检索 $_product 中不存在的属性时才这样做,否则您只是将数据库查询加倍。

->getMediaGalleryImages()

根据 Mage_Catalog_Model_Product::getMediaGalleryImages(),这 return 是 Varien_Data_Collection

->getItemByColumnValue('label','mylabel')

根据Varien_Data_Collection::getItemByColumnValue,这将仅return集合中符合您条件的单个项目。

相反,您可能想使用具有相同参数的 ->getItemsByColumnValue()

$productImages = Mage::getModel('catalog/product')
    ->load($_product->getId())
    ->getMediaGalleryImages()
    ->getItemsByColumnValue('label', 'mylabel');

从这里开始,您应该 能够循环数组 returned 并像您已经那样输出:

/** @var array $productImages */
foreach ($productImages as $productImage) { /** @var Varien_Object $productImage */
    $image = $this->helper('catalog/image')
        ->init($_product, 'image', $productImage->getFile());
    echo "<img src='" . $image . "' class='img-responsive' />";
}