WordPress.org

Ready to get started?Download WordPress

Forums

Display caption with the_post_thumbnail (12 posts)

  1. Jeremy Herve
    Member
    Posted 4 years ago #

    Hi!

    I am using the new the_post_thumbnail introduced with WordPress 2.9, and I'd have a question about it:
    I got to understand it and use it thanks to the article from Mark Jaquith, but there is one point he did not cover:
    Is it possible to display the caption below the image somehow? So far I am calling the image with <?php the_post_thumbnail();?>, but maybe there is an array to display the caption?

    Thanks for your help!

    Jeremy

  2. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    There's nothing for the post-thumb caption at the moment. However, if you use get_post_thumbnail_id($post_id) to grab the thumb's image id, you could then use get_posts to grab the image caption.

    <?php
    $thumb_id = get_post_thumbnail_id($post->id);
    $args = array(
    	'p' => $thumb_id,
    	'post_type' => 'attachment'
    	);
    $thumb_image = get_posts($args);
    $thumb_caption = $thumb_image->post_excerpt;
    ?>

    Bear in mind this is all theory and I've not had a chance to test it yet.

  3. Jeremy Herve
    Member
    Posted 4 years ago #

    I will give it a try then, and come back here to tell you if it worked!

    Should I open a ticket in the trac to propose such feature? What do you think?

    Thank you!

  4. Jeremy Herve
    Member
    Posted 4 years ago #

    It does not seem to work I am afraid.

    There is no output of the function in the code.

    I placed it in the loop, right after calling the_post_thumbnail, but no caption appeared.

    <?php the_post_thumbnail('single-post-thumbnail'); ?>
    <?php
    $thumb_id = get_post_thumbnail_id($post->id);
    $args = array(
    	'p' => $thumb_id,
    	'post_type' => 'attachment'
    	);
    $thumb_image = get_posts($args);
    $thumb_caption = $thumb_image->post_excerpt;
    ?>
    <?php the_content(); ?>

    Any idea why?

    Thank you!

  5. Jeremy Herve
    Member
    Posted 4 years ago #

    I am still stucked with that issue. Would anyone have an idea about how to solve it?

    Thanks!!

  6. danielwiener
    Member
    Posted 4 years ago #

    I got this to work on my site. You need to do a "foreach" to access items from the array created by get_posts(). And I used slightly different arguments in the get_posts(). So the code should be:

    <?php
    $thumb_id = get_post_thumbnail_id($post->id);
    		$args = array(
    	'post_type' => 'attachment',
    	'post_status' => null,
    	'post_parent' => $post->ID,
    	'include'  => $thumb_id
    	); 
    
       $thumb_images = get_posts($args);
       foreach ($thumb_images as $thumb_image) {
       echo $thumb_image->post_excerpt;
       }
    ?>

    I hope this helps.

  7. roburdick
    Member
    Posted 4 years ago #

  8. AGWD101
    Member
    Posted 4 years ago #

    I use -

    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>';
      }
    }

    Usage -

    the_post_thumbnail_caption();

  9. dsided
    Member
    Posted 3 years ago #

    Thanks AGWD101, that worked perfectly

  10. ads1018
    Member
    Posted 3 years ago #

    The problem with this solution is that the caption just floats off to the side on its own...it's not part of the thumbnail. Is there anyway to present the caption directly below the thumbnail image without having to dig into css every time you add one?

  11. wmmead
    Member
    Posted 3 years ago #

    The function AGWD101 posted works for me, but it seems to put the caption for the first post with a featured image after the title of every article that does not have a featured image.

    Any idea why?

  12. mosdave
    Member
    Posted 3 years ago #

    @wmmead:

    You need to include the 'post_parent' as an argument so it only looks at the current post.

    Here's the full function I'm using: (a combo of AGWD101 and danielwiener code)

    function the_post_thumbnail_caption() {
      global $post;
    
      $thumb_id = get_post_thumbnail_id($post->id);
    
      $args = array(
    	'post_type' => 'attachment',
    	'post_status' => null,
    	'post_parent' => $post->ID,
    	'include'  => $thumb_id
    	); 
    
       $thumbnail_image = get_posts($args);
    
       if ($thumbnail_image && isset($thumbnail_image[0])) {
         //show thumbnail title
         echo $thumbnail_image[0]->post_title; 
    
         //Uncomment to show the thumbnail caption
         //echo $thumbnail_image[0]->post_excerpt; 
    
         //Uncomment to show the thumbnail description
         //echo $thumbnail_image[0]->post_content; 
    
         //Uncomment to show the thumbnail alt field
         //$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
         //if(count($alt)) echo $alt;
      }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic