X

Featured / post thumbnail image caption

Add the first block of code to the functions.php of your wordpress theme, then add the second block within the loop of the desired template file, single.php or, index.php to display featured image caption.



Code Snippet:


function the_post_thumbnail_caption() {
global $post;

$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));

if ($thumbnail_image && isset($thumbnail_image[0])) {
echo '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
}
}


Code Snippet:


<?
the_post_thumbnail_caption();
?>

Comments  Leave a Reply

  1. Do you have a way to do this through multiple posts? We have solved the issue of showing the caption for a featured image. But, when we use that same image in a different post, all hell breaks loose.

    1. The idea would be to add this to your loop, unless I’m not following what you mean can you provide more details?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!