Displaying Customepress content only IF it's there

Guys,

Your CustomPress plugin in saving me literally hours upon hours of work. Thank you...this is a great plugin.

I love that it gives you the PHP to insert into your template right there, but I'm wondering if someone can help me out with the PHP it would take to first check if there is content in that field, and then display only if there is? Does that make sense? I'd like that field to be optional.

My other option would be to set up another custom field that'd be something like "Insert this into post" and base the PHP around the selector there, but I keep thinking there's gotta be a cleaner way to do it.

Thank you in advance...any help is appreciated!

  • Mason
    • DEV MAN’s Sidekick

    Hiya jmstovall,

    Glad you're liking the custompress plugin! We're pretty excited about it as well.

    You're wanting to wrap a conditional statement around the PHP embed code, correct? Should be the same as doing it with any other part of WordPress. I'll ask Ivan to confirm, but I think you'll be fine doing it the same way.

    There's a tutorial on conditional statements in WordPress here as well:
    http://www.addicottweb.com/2009/10/how-to-use-wordpress-conditional-statements/

    Hope that helps! Thanks!

  • jmstovall
    • WPMU DEV Initiate

    Thanks! Appreciate the direction there. Probably won't be surprised to hear I'm just not good with PHP...CSS is can swing around in pretty handily, but I need more a lot more help on the PHP side. While I could write a normal "if" statement (I think), the weird thing about this one is I want it to check if there is content entered in the box...and I don't know how to describe, in PHP, an empty box. I can see how to check if an object is an apple or an orange, but what if the fruit isn't even there?

    Anyway, this is probably more of a PHP than product question, so I understand if you can't provide support, I was just trying to find out if there was something simple I'm missing. Otherwise, I'll try and find some PHP-specific resources out there.

    Either way, good stuff! Thanks much!

  • Ivan
    • The Incredible Code Injector

    @jmstovall Hi there,

    First, check on the get_post_meta() function documentation here: http://codex.wordpress.org/Function_Reference/get_post_meta

    Second, if you use the embed code for the Custom Fields if there is nothing in the field, nothing will be outputted to the screen. If you want to explicitly test if something doesn't have a value however, use the php function empty() - http://php.net/manual/en/function.empty.php . Based on an example custom field from CustomPress this will look something like this:

    $custom_field = get_post_meta( $post->ID, '_ct_field_id', true );
    if ( empty( $custom_field ) ) {
        // Field is empty - do stuff
    } else {
        // Field is not empty - do stuff
    }

    You can put a " ! " before empty() to test if something is NOT empty. The same example from above will look like this:

    $custom_field = get_post_meta( $post->ID, '_ct_field_id', true );
    if ( ! empty( $custom_field ) ) {
        // Field is NOT empty - do stuff
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.