Php 获取包含Magento中标签的媒体库图像列表
我创建了一个可配置的产品,并添加了一些带有不同标签的图像。如何在media.phtml文件的前端中仅获取包含标签:mylabel的图像,以及何时按下此图像以获得类似于“更多视图”的功能?我有一个代码,但我不知道为什么只显示第一个图像,而不是全部Php 获取包含Magento中标签的媒体库图像列表,php,magento,magento-1.7,magento-1.9,Php,Magento,Magento 1.7,Magento 1.9,我创建了一个可配置的产品,并添加了一些带有不同标签的图像。如何在media.phtml文件的前端中仅获取包含标签:mylabel的图像,以及何时按下此图像以获得类似于“更多视图”的功能?我有一个代码,但我不知道为什么只显示第一个图像,而不是全部 $prodimg=Mage::getModel('catalog/product')->load($\u product->getId())->getMediaGalleryImages()->getItemByColumnValue('label',
$prodimg=Mage::getModel('catalog/product')->load($\u product->getId())->getMediaGalleryImages()->getItemByColumnValue('label','mylabel');
如果($prodimg!=''){
echo“helper('catalog/image')->init($\产品,'image',$prodimg->getFile())。“'class='img-responsive'/>”;
}
我还没有测试过这一点,但分解一下您的代码示例,下面是我的想法:
$prodimg = Mage::getModel('catalog/product')->load($_product->getId())
这很好,但仅当您需要检索$\u product
中不存在的属性时才执行此操作,否则您只需加倍数据库查询
->getMediaGalleryImages()
根据Mage\u Catalog\u Model\u Product::getMediaGalleryImages()
,这将返回一个Varien\u数据收集
->getItemByColumnValue('label','mylabel')
根据Varien\u Data\u Collection::getItemByColumnValue
,这将仅返回集合中符合条件的单个项目
相反,您可能希望使用具有相同参数的->getItemsByColumnValue()
:
$productImages = Mage::getModel('catalog/product')
->load($_product->getId())
->getMediaGalleryImages()
->getItemsByColumnValue('label', 'mylabel');
从这里开始,您应该能够像现在这样循环返回的数组和输出:
/** @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' />";
}
/**@var数组$productImages*/
foreach($productImages作为$productImages){/**@var Varien_对象$productImages*/
$image=$this->helper('catalog/image')
->init($_product,'image',$productImage->getFile());
回声“;
}
我还没有测试过这一点,但分解一下您的代码示例,下面是我的想法:
$prodimg = Mage::getModel('catalog/product')->load($_product->getId())
这很好,但仅当您需要检索$\u product
中不存在的属性时才执行此操作,否则您只需加倍数据库查询
->getMediaGalleryImages()
根据Mage\u Catalog\u Model\u Product::getMediaGalleryImages()
,这将返回一个Varien\u数据收集
->getItemByColumnValue('label','mylabel')
根据Varien\u Data\u Collection::getItemByColumnValue
,这将仅返回集合中符合条件的单个项目
相反,您可能希望使用具有相同参数的->getItemsByColumnValue()
:
$productImages = Mage::getModel('catalog/product')
->load($_product->getId())
->getMediaGalleryImages()
->getItemsByColumnValue('label', 'mylabel');
从这里开始,您应该能够像现在这样循环返回的数组和输出:
/** @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' />";
}
/**@var数组$productImages*/
foreach($productImages作为$productImages){/**@var Varien_对象$productImages*/
$image=$this->helper('catalog/image')
->init($_product,'image',$productImage->getFile());
回声“;
}
非常感谢你,Robbie,你的代码工作得很有魅力:)非常感谢你,Robbie,你的代码工作得很有魅力:)