Aayush Aayush - 1 year ago 108
PHP Question

Image Caption with the_post_thumbnail in WordPress

Is there a way to display the image caption where ever available when displaying

image in WordPress on the posts in the primary loop.

Thanks! Appreciate all the help.

Answer Source

I figured it out:

* Fetch The Post Thumbnail Caption

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download