隐藏特定属性-Magento

隐藏特定属性-Magento,magento,magento-1.9,Magento,Magento 1.9,我决定在视图产品的顶部插入一些属性。我希望他们在有信息的时候出现。当为空时,我希望标签和信息为空。 问题在于,当前属性为空时,即使它没有显示在标签上的任何信息 这是我的密码: <div class="short-information"> <a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')-

我决定在视图产品的顶部插入一些属性。我希望他们在有信息的时候出现。当为空时,我希望标签和信息为空。 问题在于,当前属性为空时,即使它没有显示在标签上的任何信息

这是我的密码:

<div class="short-information">
                     <a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
                       <a><span class="label"><?php echo $_product->getResource()->getAttribute('editorial')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('editorial'));?></span></a>
            </div>

我怎样才能使此代码正确工作?当attibute为空时解除标签。
请记住,这是我在页面中插入的属性代码,而不是页面视图底部的代码。

您没有指定哪个是属性,但可以使用如下if语句:

<?php if($_product->getResource()->getAttribute('autor')->getStoreLabel()): ?>
                     <a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
<?php endif ?>
if(!empty($attributeValue)) { //replace $attributeValue with your variable name.. 
//do your thing
}


其他属性也一样。

好的,为此,您需要首先检查属性是否有值,然后只允许打印标签和相应的值

<div class="short-information">
   <?php if($this->htmlEscape($_product->getData('autor'))): //checks if author attribute has some value or not ?> 
      <a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
   <?php endif; ?>
</div>


在这里,我已经检查了
author
属性值,您可以对另一个属性值执行相同的操作。

尝试使用empty或trim函数,如下所示:

<?php if($_product->getResource()->getAttribute('autor')->getStoreLabel()): ?>
                     <a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
<?php endif ?>
if(!empty($attributeValue)) { //replace $attributeValue with your variable name.. 
//do your thing
}
你也可以检查这个-

更新:尝试更改此行

 if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>


希望这能对您有所帮助。

嗨,克劳迪乌,非常感谢您的回答,但它不起作用。即使是空的,标签仍然在这里。再次感谢Claudiu,但是K.C解决方案对甲烷很有效。非常感谢你的帮助K.C,这很好;)谢谢丹尼斯给我的时间。这也是工作。非常感谢我能得到+1或接受答案,如果你从我的答案中得到帮助。