Display a Featured Image Caption in WordPress

For a recent project I was working on, I needed to display a featured image caption in a loop.

I wasn’t too sure how to go about doing this, so I thought I would share my solution with you.

The Issue I was Having with showing a Featured Image Caption

In a little more detail, I was using a plugin to create a responsive slider. I know this isn’t the best way of doing it, but it was a time saving exercise. The client needed to be able to edit the slides from the WordPress Dashboard. Not only that, but also change the links and the captions. The only problem is, as standard the plugin didn’t have the function built in to pull in the caption.

WordPress Plugins

The plugin in question is called Responsive Slider, and all in all it’s a decent plugin, I would recommend using it.

I worked out that I would have to edit the plugin to make this work, which again isn’t ideal. To resolve this issue of me or the client updating the plugin and losing the changes in code I had made, I installed another plugin called Disable WordPress Plugin Updates. This does exactly what it says on the tin (I’m sorry if you have never seen the advert – I’m not explaining it).

Solution to showing a Featured Image Caption

I scoured the interweb for a pre-written function which did the job of pulling the excerpt. Eventually I came across a novel solution by kfallman, who provided the following function for your functions.php file.

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 ''.$thumbnail_image[0]->;post_excerpt.'';
}
}

By using this function and the_post_thumbnail_caption() in your template file, its easy to display the featured image caption on your site.

Let me know how you got on, or alternatively if you have a better solution to the problem.

Leave a Reply

Your email address will not be published. Required fields are marked *