仅在数据存在时显示按钮

Only display button if data is present

我在 WordPress 中使用了这段 PHP 代码,现在它同时显示了 UK 和 DK 按钮,即使其中一个按钮只有数据。 DK 的数据使用此字段名称 "streaming_from_shop",英国使用此 "streaming_from_shop_uk"。 我怎样才能让它只显示字段中写入数据的按钮而不显示其他按钮?当然,如果两个字段中都有数据,则同时显示。

<?php
    $streaming_link = get_field('streaming_from_shop');
    if( $streaming_link ):
?>
<?php
setup_postdata($streaming_link);
?>
<a href="<?php the_field('streaming_from_shop'); ?>" class="buttonstream_shop" target="_blank">Stream DK</a>
<a href="<?php the_field('streaming_from_shop_uk'); ?>" class="buttonstream_shop" target="_blank">Stream UK</a>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

您似乎在使用高级自定义字段。 ACF 具有检查字段是否存在的功能。 https://www.advancedcustomfields.com/resources/the_field/ 所以先检查一下这个字段是否存在。

<?php if( get_field('streaming_from_shop') ): ?>
    <a href="<?php the_field('streaming_from_shop'); ?>" class="buttonstream_shop" target="_blank">Stream DK</a>
<?php endif; ?>

<?php if( get_field('streaming_from_shop_uk') ): ?>
    <a href="<?php the_field('streaming_from_shop_uk'); ?>" class="buttonstream_shop" target="_blank">Stream UK</a>
<?php endif; ?>

试试这个:

<?php if (!empty(the_field('streaming_from_shop'))) { ?>
    <a href="<?php the_field('streaming_from_shop'); ?>" class="buttonstream_shop" target="_blank">Stream DK</a>
<?php } ?>
<?php if (!empty(the_field('streaming_from_shop_uk'))) { ?>
    <a href="<?php the_field('streaming_from_shop_uk'); ?>" class="buttonstream_shop" target="_blank">Stream UK</a>
<?php } ?>