4

I'm using this function in my functions.php to display image caption for faetured images:

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 '<div class="front-caption">'.$thumbnail_image[0]->post_excerpt.'</div>';
}
} 

And using this in the template file to display the caption:

<?php 
if (the_post_thumbnail_caption()) { 
 the_post_thumbnail_caption(); 
}
?>

In the functions file I have the caption displaying in a div class="front-caption" which I am styling with borders. If the caption does not exist then it still displays the empty bordered div.

If no caption exists I do not want to display the bordered div. I just want it to display nothing.

How can I properly code this to work? Thanks in advance.

2
  • 1
    Please (A) Don't post answers as comments -> Add an answer to your own question. (B) Please rework your question. You got tons of typos.
    – kaiser
    Aug 28, 2013 at 9:21
  • There's now a plugin that creates a function for getting the caption or any other field from the thumbnail: wordpress.org/plugins/thumbnail-field
    – user52149
    May 25, 2014 at 15:13

4 Answers 4

7

I am a "little late" but this solution worked great for me. It will show the div only if the caption is not empty.

<?php
$get_description = get_post(get_post_thumbnail_id())->post_excerpt;
the_post_thumbnail();
  if(!empty($get_description)){//If description is not empty show the div
  echo '<div class="featured_caption">' . $get_description . '</div>';
  }
?>
3
  • Thanks Dejo, I'm 100% positive but I think that's what I ended up with. Oct 27, 2013 at 19:47
  • Downvote. Great but doesn't work in WP_Query for a custom loop in a page template. Apr 4, 2018 at 15:07
  • Upvote to counter @Dev because, well, you have to know how loops work to get code to work in your own specific situation, mate. Plus, downvoters are haters and Ms. Swift ain't havin' that.
    – Nathan
    Jul 29, 2020 at 21:17
3

Please note that as of WordPress 4.6, the function has been added to core (/wp-includes/post-thumbnail-template.php).

Using the code previously posted here will cause the error:

Fatal error: Cannot redeclare the_post_thumbnail_caption()

To avoid this, please name the function something else, or if catering for earlier versions of WordPress in a theme, add a check like so:

if ( ! function_exists( '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])) {
    return '<div class="front-caption">'.$thumbnail_image[0]->post_excerpt.'</div>';
   } else {
     return;
   }
 }
}
2

You could try this :

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])) {
   return '<div class="front-caption">'.$thumbnail_image[0]->post_excerpt.'</div>';
  } else {
    return;
  }
} 

and then :

echo the_post_thumbnail_caption(); 
1
  • This works great except for when I have a post thumbnail with no caption. If there is no caption it still print's the div "front-caption". I have the .front-caption styled with borders and when there is no caption it displays the post thumbnail with the empty div "front-caption" and borders. I'm trying to get the div to be hidden unless there is a caption for the post thumbnail. Aug 28, 2013 at 11:21
1

Most of these answers appear rather outdated. Please try this function:

the_post_thumbnail_caption();

Ref: https://developer.wordpress.org/reference/functions/the_post_thumbnail_caption/

For example:

<div class="featured-image-caption"><?php echo the_post_thumbnail_caption(); ?></div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.