Hiding HTML around Custom Field if empty with PHP

I’m trying to hide all the html if my custom fields are empty in WordPress

I’m using this:

Read More
<?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 } ?>

But only 1 custom field shows up, any ideas why? I’ve looked into it quite a bit but can’t figure it out

Related posts

2 comments

  1. assuming that your fields are correct (attribute_1 and value_1), then the issue with the code is the use of the incorrect functions.

    the_field outputs the contents of the field.

    In your if condition, you need to use get_field which returns the contents of the 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 } ?>
    
  2. Since you have not provided details about the methods listed above, you could try something like this.

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

    If you are banking on ‘myfield’ being variables from a different page, then you should use:

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

Comments are closed.