"If else" functions on custom fields

EDIT: This is for the custom press plugin. The Plugin page directs to the home of the BB press forums and won't let me sort it into the right place.

I am looking for a way to implement some sort of IF ELSE function on custom post queries. For example, I want to be able to have a custom field that allows for an override of a default header image. If the field for the URL is left empty on the post editor the page will just show nothing. I want there to be a hardcoded default in the theme template that is a fallback of sorts for these options.

  • Mark

    You are on the right track.

    What you need to do is check if that field is empty or not. In order to do that you have to retrieve that field as a variable. To do this, you can use the get_post_meta() function.

    This function has three parameters: post_id, key, single. Read more about the function here: http://codex.wordpress.org/Function_Reference/get_post_meta

    So if you wanted to get the value of a field with an id of "_ct_text_5229741c245d2" you would use the following code:

       $property = get_post_meta( $post->ID, '_ct_text_5229741c245d2', true );

    The value of that field is stored in the variable $property. Now all you have to do is run a simple conditional statement to check if that field is empty or not.

    if( !empty( $property ) ) {
     echo $property;
     echo 'something else';

    You should be able to put two and two together and accomplish what you are trying to do.

    Hope this helps.

  • Alexander

    Hi there,

    Just to offer another perspective here, you can also do this with a filter that CustomPress makes available in the shortcode handler. You can always filter anything that comes out of that shortcode.

    So for example you could do something like this:

    function custom_field_fallback($result, $atts, $content) {
    	if( $result )
    		return $result;
    		return "Fallback...";

    You'd have access to the data you need to test any condition, then you can return a fallback, or pretty much do anything you want in there.

    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.