如果两个回声具有相同的值,我可以让我的输出只显示 1 吗?



好吧,在我的网站上,我有一个模板,显示有关我的WooCommerce产品的信息。它将在表中显示属性。现在,我有一些在两个属性类别中显示的属性,因为我对这些属性具有相同的名称。

目前我将属性显示为:

<td style="width: auto;"><?php 
                $product = new WC_Product(get_the_ID()); ?>
                <a href="<?php the_permalink() ?>" target="_blank">
                <?php echo $product->get_attribute('pa_voedings-spanning-dc-ac'); ?>
                <text>-</text>
                <?php echo $product->get_attribute('pa_voedings-spanning-dcac'); ?>
                </a>
            </td>

现在,如果我的属性具有相同的值,那么它只显示其中1个?稍后,我将尝试弄清楚为什么有时会在第一个属性中显示它,有时在第二个属性中或在两个属性上显示的某些产品,但我认为这可能是一种选择。

非常感谢您的帮助!

您之前可以获得属性,然后在写之前进行比较:

<td style="width: auto;"><?php
    $product = new WC_Product(get_the_ID());
    $attr1 = $product->get_attribute('pa_voedings-spanning-dc-ac');
    $attr2 = $product->get_attribute('pa_voedings-spanning-dcac');
    ?>
    <a href="<?php the_permalink() ?>" target="_blank">
    <?php echo $attr1 ; ?>
    <?php if ($attr2 != $attr1) : ?>
        <text>-</text><?php echo $attr2; ?>
    <?php endif; ?>
    </a>
</td>

最新更新