WordPress – Get Post Thumbnail Caption

If you are looking for the way to get post thumbnail/featured image caption value in WordPress, this post is for you.

<?php

function get_post_thumbnail_caption()  {
    $post_thumbnail_caption = '';
    if ( $post_thumbnail_id = get_post_thumbnail_id() ) {
        $post_thumbnail_caption = get_post( $post_thumbnail_id )->post_excerpt;
    }
    return $post_thumbnail_caption;
}

In the above code, we get the post thumbnail id of the current post, get the post by this thumbnail id, and return post_excerpt property which contains caption value.

 

Use the function:

echo get_post_thumbnail_caption();

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x