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!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.