• Hello,

    I am using this code for displaying post custom field value

    <?php echo "Model: "; $key_name = get_post_custom_values($key = 'Model');echo $key_name[0]; ?>
    and some others.

    Sometimes post does not have all custom fields and there is displaying just text “ModeL:”
    How can I change the code above in order to display nothing when custom fields model is blank.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello
    you can use this function

    echo “Model: “.get_post_meta(‘your-post-id-here’, ‘Model’, true);

    Thread Starter janbrokes

    (@janbrokes)

    thanks for quick reply.

    i need instead of ‘your-post-id-here use curent post id, beacuse it will be displayed on thousands of posts detail

    To get the current post ID, you can use function get_the_ID(). So the code is:

    echo 'Model: ', get_post_meta( get_the_ID(), 'Model', true );

    By the way, if you need a better way to create custom fields, there’s a plugin Meta Box that can help you a lot.

    you can aslo check if the field is filled in before you output:

    <?php $key_name = get_post_custom_values($key = 'Model');
    if( $key_name[0] ) { echo "Model: "; echo $key_name[0]; } ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom field with’ is closed to new replies.