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.

Good evening!

I'm trying to hide the featured image caption from showing. Currently, it'll show build the div (but not populate it because it's empty). What I'd like to do, ideally, is only show the div if someone has entered text in the Caption (post_excerpt) box in the Media Panel.

This is the code I'm working off of:

    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 '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
      }
    }

This is what I've attempted on my own to try and hide the caption if it's empty:

    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'));
      $thumbnail_caption = $thumbnail_image[0]->post_excerpt;
      $thumbnail_set = isset($thumbnail_image[0]);

        if (! empty($thumbnail_set)) {
          if ($thumbnail_image && isset($thumbnail_image[0])) {
            echo '<div class="img-caption"><h5>'.$thumbnail_caption.'</h5></div>';
          } else return;
      } else return;
    }

This is the code in my page template:

    <section class="entry-content">
        <?php if ( has_post_thumbnail() ) : ?>
            <figure class="featured-img alignright">
                <?php the_post_thumbnail('med_size'); ?>
                <?php the_post_thumbnail_caption(); ?>
            </figure><!-- .entry-page-image -->
        <?php endif; ?>
      <?php the_content(); ?>
    </section>

When I delete the caption on the featured image. It goes away, but the div is still being created as well as the emtpy h5 element.

Here are screen captures of the two states:

link to when a caption is left blank

here is a link when a caption is filled in

Thanks for the advice and help!

share|improve this question
    
In 'if ($thumbnail_image && isset($thumbnail_image[0])) {' - should '$thumbnail_image[0]' not be '$thumbnail_caption' –  Magenta Cuda Feb 25 '13 at 1:50
    
Sweet, that mixed it @MagentaCuda! Thanks! Add that as a response and I'll up-check you. –  Rick Pickett Feb 25 '13 at 2:23
    
Glad to be of help - don't care about the points. –  Magenta Cuda Feb 25 '13 at 3:32
1  
Please still consider adding it as an answer, it helps when a question is closed as answered, it's not just about points. –  Wyck Feb 25 '13 at 4:48
    
Didn't think about that - will do. –  Magenta Cuda Feb 25 '13 at 6:05

1 Answer 1

up vote 1 down vote accepted

In 'if ($thumbnail_image && isset($thumbnail_image[0])) {' - should '$thumbnail_image[0]' not be '$thumbnail_caption'.

share|improve this answer

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.