Family Encyclopedia >> Electronics

How to Display WordPress Featured Images with Captions and Descriptions

WordPress Featured Images, also known as Post Thumbnails, let you add rich metadata like titles, captions, and descriptions during upload. By default, only the image appears on your site. As seasoned WordPress developers, we've used this reliable technique countless times to display those details alongside thumbnails, enhancing your theme's professionalism.

First, enable post thumbnails in your theme's functions.php file:

<?php add_theme_support( 'post-thumbnails' ); ?>

Now, when uploading images via the Media Library, fill in the Title, Caption, Alt Text, and Description fields, as shown:

How to Display WordPress Featured Images with Captions and Descriptions

WordPress treats each image attachment as its own post: the Title becomes post_title, Caption is post_excerpt, and Description is post_content. This makes it easy to retrieve and display them.

To show your post thumbnail with its caption inside the Loop, use this code:

<?php
if ( has_post_thumbnail() ) {
    $thumb_id = get_post_thumbnail_id();
    $caption = get_post( $thumb_id )->post_excerpt;
    the_post_thumbnail();
    if ( $caption ) {
        echo '<p class="thumbnail-caption">' . esc_html( $caption ) . '</p>';
    }
}
?>

Style the caption with CSS for perfect integration.

For the full image description, add this inside the Loop:

<?php
$thumb_id = get_post_thumbnail_id();
$description = get_post( $thumb_id )->post_content;
if ( $description ) {
    echo '<div class="image-description">' . apply_filters( 'the_content', $description ) . '</div>';
}
?>

This straightforward method elevates your custom WordPress themes, providing context-rich visuals trusted by top sites.