繁体   English   中英

如果使用php声明并在wordpress中推进自定义字段-隐藏空字段

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM