简体   繁体   English

PHP隐藏自定义字段(WordPress)

[英]PHP Hide Custom Field (WordPress)

I am having trouble getting the field "d4" to be hidden when that field is empty. 当该字段为空时,我很难隐藏该字段“ d4”。 I am using WordPress with the plugin advance custom fields http://www.advancedcustomfields.com/ . 我正在将WordPress与插件高级自定义字段http://www.advancedcustomfields.com/结合使用 However with the code below it will display the field and its title whether or not it is empty or filled. 但是,使用下面的代码,无论该字段为空还是已填充,它将显示该字段及其标题。 THANK YOU IN ADVANCE! 先感谢您!

do_action( 'genesis_before_sidebar_widget_area' );

echo '<section class="widget member-bio-meta">';
echo '<div class="widget-wrap">';

echo "<h4>A1</h4>";
the_field('a1');

echo "<h4>B2</h4>";
the_field('b2');

echo "<h4>C3</h4>";
the_field('c3');

echo "<h4>D4</h4>";
the_field('d4');

echo '</div></section>';

Others on the net have suggested the following but I am not sure how to get it to work for < ?php throws an error "Parse error: syntax error, unexpected '<' in": 网上的其他人已经提出了以下建议,但是我不确定如何使它在<?php中起作用:抛出错误“解析错误:语法错误,意外的'<'in”:

<?php if( get_field('field_name') ): ?>
    <p>My field value: <?php the_field('field_name'); ?></p>
<?php endif; ?>

or this method 或这种方法

<?php if( $field = get_field('artikkelforfatter') ): ?>
    <p class="tittelboks">Artikkelforfatter:</p>
    <p><?php echo $field; ?></p>
<?php endif; ?>

You should use get_field() function instead. 您应该改用get_field()函数。 This function will return a value, and you can do a check to see if the value is empty or not. 该函数将返回一个值,您可以检查该值是否为空。 Like this: 像这样:

$d4 = get_field('d4');
if (!empty($d4)){
    echo "<h4>D4</h4>";
    echo $d4;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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