How to Display WordPress Post Thumbnails with Captions

138

WordPress has this amazing feature known as Post Thumbnails also referred to as Featured Images. When uploading a post thumbnail, you are asked to add a caption, description, along with other fields, but when you get ready to display it, it only shows the image. In this article, we will share a small trick that will let you display captions or entire image descriptions with your post thumbnail.

First thing first, you need to enable Post Thumbnails in your WordPress Theme. Now, when you upload an image using the media uploader, you will see something like this:

Add an Image

You will need to go ahead and fill all of the information. Now one thing that many folks do not know is that WordPress stores each image as its own post. So the Title of the Image will be the title of the post, Caption will be the excerpt of the post, and Description will be the content of the post. So you can display all of these information when you are displaying your WordPress Thumbnail.

To display your post thumbnail with its caption, simply paste the following code inside the loop:

<?php the_post_thumbnail();  echo get_post(get_post_thumbnail_id())->post_excerpt; ?>

You can obviously style it the way you want to style it.

You can also display entire image description by adding this code inside the post loop:

<?php the_post_thumbnail();  echo get_post(get_post_thumbnail_id())->post_content; ?>

This really simple trick can go long way in styling your next custom WordPress theme.