PHP Hide Custom Field (WordPress)

I am having trouble getting the field “d4” to be hidden when that field is empty. I am using WordPress with the plugin advance custom fields 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”:

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

Related posts

Leave a Reply

1 comment

  1. You should use get_field() function instead. 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;
    }