The get_the_post_thumbnail_caption() WordPress PHP function retrieves the caption of a post thumbnail.
Usage
To use the get_the_post_thumbnail_caption() function, simply pass the post ID or WP_Post object as the parameter. If no parameter is passed, it will use the global $post
by default.
echo get_the_post_thumbnail_caption($post_id);
Parameters
$post
(int|WP_Post) – Optional. Post ID or WP_Post object. Default is global$post
.
More information
See WordPress Developer Resources: get_the_post_thumbnail_caption()
Examples
Display the thumbnail caption for the current post
In this example, the thumbnail caption for the current post will be displayed.
echo get_the_post_thumbnail_caption();
Display the thumbnail caption for a specific post by ID
In this example, we will display the thumbnail caption for a specific post with the ID 42.
$post_id = 42; echo get_the_post_thumbnail_caption($post_id);
Display the thumbnail caption for a WP_Post object
In this example, we will display the thumbnail caption for a WP_Post object.
$wp_post = get_post(42); echo get_the_post_thumbnail_caption($wp_post);
Check if a post thumbnail has a caption before displaying it
In this example, we will check if a post thumbnail has a caption before displaying it.
$caption = get_the_post_thumbnail_caption(); if (!empty($caption)) { echo $caption; }
Display the thumbnail caption with custom HTML
In this example, we will display the thumbnail caption within custom HTML.
$caption = get_the_post_thumbnail_caption(); echo '<span class="thumbnail-caption">' . $caption . '</span>';