Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
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 '13 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 –  Drywall May 25 at 15:13
add comment

2 Answers 2

up vote 0 down vote accepted

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_post(get_post_thumbnail_id())->post_excerpt . '</div>';
  }
?>
share|improve this answer
    
Thanks Dejo, I'm 100% positive but I think that's what I ended up with. –  Paul Coppock Oct 27 '13 at 19:47
add comment

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(); 
share|improve this answer
    
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. –  Paul Coppock Aug 28 '13 at 11:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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