[英]If statement using php and advances custom fields in wordpress - hide empty fields
我有一個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; ?>
有人有建議嗎?
干杯:)
我不是ACF專家,但在這里查看get_field()函數描述https://www.advancedcustomfields.com/resources/get_field/看起來該函數將永遠不會返回布爾值,如描述中所述,我引用:
返回指定字段的值
由於它不返回布爾值,因此無法保證get_field('something')&& get_field('something2')將是正確的布爾值。 if語句將某些值解釋為布爾值true或false。 例如,null和0解釋為false,但是-1解釋為true。 我建議做一個
var_dump( get_field('button_link') )
探索輸出。 另外,根據https://www.advancedcustomfields.com/resources/get_field/在“檢查值是否存在”下,您可以檢查一個值是否存在,因此這可能有效:
<?php if ( get_field( 'button_link' ) ) : ?>
<?php if ( 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 ?>
<?php endif ?>
它就像一個嵌套的AND,而無需使用&&運算符。 如果這不起作用,我們需要更多有關您從中獲得的信息:
var_dump( get_field( 'button_link' ) );
var_dump( get_field( 'button_text' ) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.