if语句使用php并在wordpress中提升自定义字段 - 隐藏空字段
I have an and if statement for a button.
If both advanced custom fields are present I want it to show the button and if not I want it to hide but I am struggling here.
I have looked at this page:
https://www.advancedcustomfields.com/resources/hiding-empty-fields/
Here is my code:
<?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; ?>
Anyone got a suggestion please?
Cheers :)
我有一个按钮的if和if语句。 p>
如果两个都是高级的 自定义字段存在我希望它显示按钮,如果不是我希望它隐藏但我在这里苦苦挣扎。 p>
我看过这个页面: p> \ n
https://www.advancedcustomfields.com/resources/hiding-empty -fields / p>
这是我的代码: p>
&lt;?php if(get_field('button_link')&amp; ;&amp; get_field('button_text')):?&gt;
&lt; a href =“&lt;?php the_field('button_link');?&gt;” class =“btn third-btn mx-auto”&gt;
&lt;?php the_field('button_text');?&gt; &lt; i class =“fa fa-arrow-circle-right”aria-hidden =“true”&gt;&lt; / i&gt;
&lt; / a&gt;
&lt;?php endif; ?&gt;
code> pre>
任何人都有建议吗? p>
干杯:) p>
div>
I am not an ACF expert but looking at the get_field() function description here https://www.advancedcustomfields.com/resources/get_field/ it looks like the function will never return a boolean as mentioned on the Description and I quote:
Returns the value of the specified field
Since it does not return a boolean value, you can't assure get_field( 'something' ) && get_field( 'something2' ) will be the correct boolean. There are certain values that the if statement interpret as boolean true or false. For example null and 0 are interpreted as false, but -1 interpreted as true. I would recommend to do a
var_dump( get_field('button_link') )
to explore the output. Also, according to https://www.advancedcustomfields.com/resources/get_field/ under 'Check if value exists' you can check if one value exists, so this might work:
<?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 ?>
It is like a nested AND without using the && operator. If this does not work we need more information about what you get from:
var_dump( get_field( 'button_link' ) );
var_dump( get_field( 'button_text' ) );