Php Magento multi-select属性-如果仅为产品选择了一个选项,则不会在前端显示

Php Magento multi-select属性-如果仅为产品选择了一个选项,则不会在前端显示,php,magento,attributes,multi-select,Php,Magento,Attributes,Multi Select,显示多选属性选项时出现问题: catalog/product/list.phtml中使用的以下代码可以完美地显示所选属性-但仅当选择了多个选项时- 因此,如果只选择了“多重选择”属性中的一个选项,它不会显示任何内容 <?php $targetValues = $_product->getAttributeText('ni_featured_logo_multi'); foreach($targetValues as $_target) :?> <div class="fe

显示多选属性选项时出现问题: catalog/product/list.phtml中使用的以下代码可以完美地显示所选属性-但仅当选择了多个选项时- 因此,如果只选择了“多重选择”属性中的一个选项,它不会显示任何内容

<?php
$targetValues = $_product->getAttributeText('ni_featured_logo_multi');
foreach($targetValues as $_target) :?>
<div class="featuredlogolist">
<span class="helper"></span>
<img src="<?php echo $this->getSkinUrl() ?>FEATURED_LOGOS/<?php echo $_target ?>.png" class="featuredlogo"></img>
</div>
<?php endforeach;
?>

getSkinUrl()?>特色标识/.png“class=“featuredlogo”>
产品页面也是如此(catalog/product/view.phtml中使用的代码)


getSkinUrl()?>特色标识/.png“class=“featuredlogo”>

关于如何调整调用以在仅选择1个选项时显示multi-select属性,您有何想法?谢谢

问题是
getAttributeText()
实际上仅当存在多个选项时才返回数组,否则它只返回单个选项作为字符串文本。我认为这里的方法声明实际上是错误的,但我可以根据经验确认这是一种行为

您应该添加一个简单的检查,如下所示:

if ($targetValues = $_product->getAttributeText('ni_featured_logo_multi')) {
    if (is_string($targetValues)) {
        $targetValues = array($targetValues);
    }
    foreach ($targetValues as $_target) ...
}

想要发布工作代码-使用fantasticrice的编辑: 目录/product/list.phtml中的多选:(这是从皮肤文件夹中获取图像名称)


getSkinUrl()?>特色标识/.png“class=“featuredlogo”>
在catalog/product/view.phtml中:

  <?php
    if ($multiSelectArray = $this->getProduct ()->getAttributeText('your_attribute_code')) {
    if (is_string($multiSelectArray)) {
        $multiSelectArray = array($multiSelectArray);
    }
    foreach ($multiSelectArray as $multiSelectItem) :?>
   <img src="<?php echo $this->getSkinUrl() ?>FEATURED_LOGOS/<?php echo $multiSelectItem ?>.png" class="featuredlogo"></img>
   <?php endforeach;

                    }
    ?>

getSkinUrl()?>特色标识/.png“class=“featuredlogo”>

谢谢你,莱斯

谢谢你的回复不能让代码工作吗?你能详细说明一下如何在产品和产品清单上使用它吗?谢谢你的邀请help@chrismmmmm-如果在代码中
var\u dump($targetValues)
会发生什么?
 <?php if ($targetValues = $_product->getAttributeText('your_attribute_code')) {
    if (is_string($targetValues)) {
        $targetValues = array($targetValues);
    }
        foreach($targetValues as $_target) :?>
         <div class="featuredlogo">
         <img src="<?php echo $this->getSkinUrl() ?>FEATURED_LOGOS/<?php echo $_target ?>.png" class="featuredlogo"></img>
         </div>
        <?php endforeach;
                 }
    ?>
  <?php
    if ($multiSelectArray = $this->getProduct ()->getAttributeText('your_attribute_code')) {
    if (is_string($multiSelectArray)) {
        $multiSelectArray = array($multiSelectArray);
    }
    foreach ($multiSelectArray as $multiSelectItem) :?>
   <img src="<?php echo $this->getSkinUrl() ?>FEATURED_LOGOS/<?php echo $multiSelectItem ?>.png" class="featuredlogo"></img>
   <?php endforeach;

                    }
    ?>