1

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!

  • In 'if ($thumbnail_image && isset($thumbnail_image[0])) {' - should '$thumbnail_image[0]' not be '$thumbnail_caption' – user27457 Feb 25 '13 at 1:50
  • Sweet, that mixed it @MagentaCuda! Thanks! Add that as a response and I'll up-check you. – tigre Feb 25 '13 at 2:23
  • Glad to be of help - don't care about the points. – user27457 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. – user27457 Feb 25 '13 at 6:05
1

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

1

As of WordPress 4.6, the function the_post_thumbnail_caption() has been added to core (/wp-includes/post-thumbnail-template.php).

Using the code posted here will cause the error:

Fatal error: Cannot redeclare the_post_thumbnail_caption()
  • That's a good example why we shouldn't define custom functions with such general names ;-) – birgire Aug 19 '16 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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