使用 PHP 将 HTML 隐藏在自定义字段周围(如果为空)


Hiding HTML around Custom Field if empty with PHP

如果我的自定义字段在 Wordpress 中为空,我正在尝试隐藏所有 html

我正在使用这个:

<?php if (the_field( 'attribute_1' ) & the_field( 'value_1' ) != "") { ?>
<li style="line-height:2.25em;border-top:1px solid #dcdcdc;"><p style="font-size:15px;margin-bottom:0;"><?php the_field( 'attribute_1' ); ?> <span style="float:right;font-weight:600;font-family:'Varela Round';"><?php the_field( 'value_1' ); ?></span></p></li>
<?php } ?>

但是只显示 1 个自定义字段,有什么想法吗?我已经研究了很多,但无法弄清楚

假设您的字段是正确的(attribute_1value_1),那么代码的问题是使用了不正确的函数。

the_field输出字段的内容。

if条件下,您需要使用 get_field 返回字段的内容:

<?php if (get_field( 'attribute_1' ) && get_field( 'value_1' ) != "") { ?>
    <li style="line-height:2.25em;border-top:1px solid #dcdcdc;">
        <p style="font-size:15px;margin-bottom:0;"><?php the_field( 'attribute_1' ); ?> <span style="float:right;font-weight:600;font-family:'Varela Round';"><?php the_field( 'value_1' ); ?></span>
        </p>
    </li>
<?php } ?>

由于您尚未提供有关上面列出的方法的详细信息,因此您可以尝试这样的事情。

<?php
if(isset("myfield") && isset("myfield2")){
   echo "<input type='"text'" id='"customField1'"></input>";
} //repeat as necessary
?>

如果您指望"myfield"是来自不同页面的变量,那么您应该使用:

<?php //use $_GET if necessary
if(isset($_POST['myfield']) && isset($_POST['myfield2'])){
   echo "<input type='"text'" id='"customField1'"></input>";
} //repeat as necessary
?>