• Hi,

    I want to show a div when there are no Custom fields (CF) present in a post. I know how to do a check if one particular CF is present or not, but that’s just inefficient if I have to check them all one by one.

    Can anybody help me?

    Alex

Viewing 1 replies (of 1 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with get_post_custom_keys().
    http://codex.wordpress.org/Function_Reference/get_post_custom_keys

    // inside the loop
    $custom_field_keys = get_post_custom_keys( $post->ID );
    if ( empty( $custom_field_keys ) ) {
    	echo 'no custom fields found';
    }

    If a plugins or theme has also added custom fields to posts you’ll need to check for your custom fields only.

    $keys_found = false;
    $post_custom_fields = get_post_custom_keys( $post->ID );
    
    // add your custom field keys here
    $keys_to_check = array( 'custom_field_key_1', 'custom_field_key_2', 'etc...' );
    
    foreach ( $post_custom_fields as $key ) {
    	if ( in_array( $key , $keys_to_check ) ) {
    		$keys_found = true;
    		break;
    	}
    }
    
    if ( $keys_found ) {
    	echo 'custom fields found';
    } else {
    	echo 'no custom fields found';
    }

Viewing 1 replies (of 1 total)
  • The topic ‘Display message when there are no Custom fields in post’ is closed to new replies.