1

我正在使用此代码在 Magento 中显示(在前端)产品的所有分配属性:

<?php if ($_product->getData('metal')): ?>  
    <?php echo nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product )) ?>  
<?php endif; ?>

我需要用一个要点&#149;和一个 break来分隔每个项目<br/>。我将如何编辑此代码以反映这些更改?提前致谢。

_山姆

4

3 回答 3

1

如果您想对输出进行更多控制(也许您想设置单个值的样式),请使用 explode();

<?php $attr = explode("," ,  $_helper->productAttribute($_product, $_data['value'], $_data['code'])); 
echo "<ul>";
?>
<?php foreach ($attr as $attrValue) {
    echo "<li>" . $attrValue . "</li>";
} 
echo "</ul>";
?>
于 2015-08-04T08:39:26.383 回答
0

解决了。这很容易:如果您的默认包中没有,则从 base 复制 attributes.phtml。

放入 app/design/frontend/default/default/template/catalog/product/view/ 文件夹

将第 46 行从:

<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>

显示单独的产品属性:

<td class="data"><?php echo $_helper->productAttribute($_product, str_replace(",", "<br />", $_data['value']), $_data['code']) ?></td> 
于 2013-09-02T13:09:09.273 回答
0

假设上面的代码是“工作”......做这个改变:

<?php echo "&#149;".nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product ))."<br/>" ?>

the.是一个字符串连接运算符。

或者如果以上是产品列表的 HTML,您是否在问如何做到这一点?那么这应该工作......

<?php 
$somehtml=nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product ));
$somehtml=explode("<br />",$somehtml);  // This ASSumes nl2br inserts <br /> between each line and this 
                                        // would lead to the behaviour you want.
                                        // see: http://php.net/manual/en/function.nl2br.php
$somehtml=implode("<br/>\n&#149;",$somehtml);
$somehtml="&#149;".$somehtml."<br/>\n";
echo $somehtml;
unset($somehtml);
?>

explode通过"<br />". implode然后通过放置<br/>\n&#149;在每个元素之间重新加入字符串。最后,我将项目符号点添加到前面并在末尾添加 br。内爆时,您仍然必须考虑字符串的“外部”。

于 2012-05-11T17:14:38.270 回答