How to Echo a custom field title only when filled?

I have very basic php skills at the moment. I’m using a WordPress plugin called Advanced Custom Fields. I have the basics down and have it working. It is echoing the results of the filled in field. What I want to do is have the title of the field only show if the field has been filled. For example, if the user has filled out the field for (number of bedrooms) and chooses 4. The Echo would show as Bedrooms: 4

But, if the user does not fill in that part of the form, I don’t not want it to show Bedrooms:

Read More

This is what I’m using right now, how can I hide that Bedrooms:? in an if statement?

 <?php if ( !is_front_page() ) { ?>
      <div class="post-list_h">
      <?php } ?>

       <?php if ( $instance['bedrooms'] ) : ?>
        <h4 class="customFields_widget">Bedrooms: <?php the_field('bedrooms'); ?></h4>
      <?php endif; ?>

Related posts

1 comment

  1. Try this :

    <?php if ( !is_front_page() ) { ?>
        <div class="post-list_h">
    <?php } ?>
    
    <?php if ($instance['bedrooms'] && !empty(get_field('bedrooms'))) { ?>
        <h4 class="customFields_widget">Bedrooms: <?php the_field('bedrooms'); ?></h4>
    <?php } ?>
    

    What is considered empty according to the Php doc is :

    • “” (an empty string)
    • 0 (0 as an integer)
    • 0.0 (0 as a float)
    • “0” (0 as a string)
    • NULL
    • FALSE
    • array() (an empty array)
    • $var; (a variable declared, but without a value)

Comments are closed.