Support

Account

Home Forums General Issues Custom field display on "php if" variable Reply To: Custom field display on "php if" variable

  • That should work fine, and the alternate if(): endif; structure is a bit cleaner than the echo code which will do the same thing (even though my example had it in a mixed-up order).

    Analyzing the code structure line by line:

    <?php if( get_field( "review_summary" ) ): ?>
    We check with PHP to see if the custom field review_summary returns any results at all. If the field is not populated, the next few lines of code gets skipped entirely. Nothing is printed or echoed here.

    <h3><?php _e('Summary'); ?></h3>
    Prints the title “Summary” in an <h3 /> tag. This only happens if the previous if() statement validates.

    <p><?php the_field( "review_summary" ); ?></p>
    Prints the review_summary field. Unlike the initial get_field("review_summary") field call, this one actively outputs its contents without having to set an echo in front of it.

    <?php endif; ?>
    Closing if statement.

    Sometimes it helps to indent code to get a better view of the flow:

    <?php if( get_field( "review_summary" ) ): ?>
    	<h3><?php _e('Summary'); ?></h3>
    	<p><?php the_field( "review_summary" ); ?></p>
    <?php endif ?>

    Forgive me if this is too obvious – just trying to help answer any questions you might have. Good luck!