我有一個和if語句的按鈕。如果語句使用php和高級自定義字段在wordpress - 隱藏空字段
如果兩個高級自定義字段都存在,我希望它顯示按鈕,如果沒有,我希望它隱藏,但我在這裏掙扎。
我已經看過此頁:
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()來代替,似乎更清楚我,但檢出這裏「值存在」選項:https://開頭WWW .advancedcustomfields.com/resources/get_field/- 您的button_link和button_text字段可能會返回空數組或字符串,而不是布爾值false - 因此它將評估您的if塊。 – JDev518
鏈接和文本都需要填充以便語句評估爲true。你是說即使這些值是空的,你的按鈕也會呈現嗎?我會調試這個語句(xdebug,var_dump或者echo'get_field('button_link')'和'get_field('button_text')')的結果,因爲它們必須包含一個真正的值(boolean true,非空字符串,或者正整數) –
我沒有看到任何錯誤的代碼它應該顯示按鈕時,button_link和button_text字段不是空的,使用print_r(get_filed('button_link'));和print_r(get_filed('button_text'));看到如果他們有價值 –