【发布时间】:2017-06-05 17:46:30
【问题描述】:
我有一个按钮的 and if 语句。
如果两个高级自定义字段都存在,我希望它显示按钮,如果没有,我希望它隐藏,但我在这里挣扎。
我看过这个页面:
https://www.advancedcustomfields.com/resources/hiding-empty-fields/
这是我的代码:
<?php if( get_field('button_link') && get_field('button_text') ): ?>
<a href="<?php the_field('button_link'); ?>" class="btn third-btn mx-auto">
<?php the_field('button_text');?> <i class="fa fa-arrow-circle-right" aria-hidden="true"></i>
</a>
<?php endif; ?>
请问大家有什么建议吗?
干杯:)
【问题讨论】:
-
我喜欢使用 get_post_meta() 代替,对我来说似乎更清楚,但在这里检查“值存在”选项:advancedcustomfields.com/resources/get_field - 您的 button_link 和 button_text 字段可能返回一个空数组或字符串,不是布尔值 false - 因此它会评估您的 if 块。
-
需要填充链接和文本,才能使语句评估为真。您是说即使这些值为空,您的按钮也会呈现?我会调试该语句(xdebug、var_dump 或回显
get_field('button_link')和get_field('button_text')的结果,因为它们必须包含一个真值(布尔真、非空字符串或正整数)。 -
我发现你的代码没有问题,当 button_link 和 button_text 字段不为空时,它应该显示按钮,使用 print_r(get_filed('button_link'));和 print_r(get_filed('button_text'));看看他们是否有价值观
-
感谢所有 cmets 人! Máximo 为我整理了 :) 干杯!!
标签: php wordpress advanced-custom-fields