Support

Account

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

Solved

Custom field display on "php if" variable

  • I tried posting on the WordPress.org forums for help with this but as usual no support comes out of there.

    I’m trying to display a custom field, through the ACF plugin… a summary, on pages that include a review box for my posts.

    The bit of code that includes the review template if it is checked while making the post looks like this:

    <?php $result_cmb_is_review = get_post_meta($post->ID, 'cmb_is_review', true);
    if ($result_cmb_is_review == 'checked')
    {include 'inc/template_review.php';
    }
    ?>

    I’d like to add this code for a summary box after the review box in the post with this in it:

    <h3><?php _e(‘Summary’); ?></h3>
    <p><?php get_field( “review_summary” ); ?></p>

    (unless there’s a better way of displaying the ACF field)

    But I only want the Summary field to show up if the post is checked to be a review. I can’t seem to figure out how to get both the template and the custom field to show up only if the review is checked. Any tips would be wonderful.

  • I might be missing the point here, but…

    <?php
    $result_cmb_is_review = get_post_meta($post->ID, 'cmb_is_review', true);
    if ($result_cmb_is_review == 'checked'){
    	include 'inc/template_review.php';
    	echo '<h3>'._e('Summary').'</h3>';
    	echo '<p>'.get_field("review_summary").'</p>';
    }

    Is this what you mean?

  • Hi @gimpy2k7

    Also, please do not use the get_post_meta function. Instead, use the get_field function as documented here:

    http://www.advancedcustomfields.com/resources/functions/get_field/

  • Hi guys I think I figured it out while tinkering last night.
    What I’ve done is add the summary field as a required entry for posts in the Reviews category (does ACF also apple that to subcategories in that main category?) by setting the rules as
    Post category > is equal to > Reviews
    and added this code to my page:

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

    From what I see it seems to be working. If you guys notice that isn’t going to achieve what I’m looking for please let me know, or if the echo code above is a better choice. I tried that code and it didn’t put the proper h3 text style on the title.

    But from what I can see it adds the summary field and h3 title only to posts that have something in the summary field. Is that right? Or is it trying to display it regardless, just without content? I guess that wouldn’t make sense because the title Summary would show up if that were the case.

    Thanks.

  • 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!

  • No worries. I appreciate you taking the time to write that out, it explains some things I had an idea of but not entirely sure on and it helps a lot.

    I’m also glad that that code will work…frustrating trying to figure it out when you haven’t done it before.

  • Cool, glad to help! We’re all (forever) learning, and I agree that the WP Forums don’t always get the best results. Thanks for stopping by 🙂

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Custom field display on "php if" variable’ is closed to new replies.