我决定在视图产品的顶部插入一些属性。我希望他们在有信息的时候出现。空的时候我要标签和信息消失。问题是,当当前属性为空时,即使它没有任何信息,它也会显示为标签。
这是我的代码:
<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>
请问我如何才能使此代码正确工作?当瓶子是空的时候解开标签。请记住,这是我在页面中插入的属性代码,而不是页面视图底部的属性代码。
您没有指定哪个是属性,但您可以使用这样的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 ?>
其他属性也是如此。
好的,为此,您需要首先检查属性是否有值,然后只允许打印标签和相应的值。
<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
属性值,您可以对其他属性值执行同样的操作。