1

I use this code to display featured image title, caption (excerpt), description (content):

echo get_post(get_post_thumbnail_id())->post_title; echo get_post(get_post_thumbnail_id())->post_excerpt; echo get_post(get_post_thumbnail_id())->post_content;

But, how to check if there is any of those fields first?

For example, how to check if there is caption, and if emty to show some other text?

Best regards.

1

It's more a programming question, but try this:

$post = get_post( get_post_thumbnail_id() );
if ( !isset( $post->post_title ) ) {
    echo 'Sorry no title';
} else {
    echo $post->post_title;
}
0

Use if statements.

if($caption = get_post(get_post_thumbnail_id())->post_excerpt){
echo $caption;
}else{
//echo something else
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.