Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to display the caption from the_post_thumbnail? WordPress

  • SOLVED

Hello,

I have the_post_thumbnail(); in my template and want to display the caption, alt text, title and description for the associated image. How do I retrieve and echo these values?

Thanks in advance,
Matt

Answers (3)

2011-01-10

Sébastien | French WordpressDesigner answers:

add this
if(has_post_thumbnail()) {

$thumb_id = get_post_thumbnail_id();
$args = array(
'ID' => $thumb_id,
'post_type' => 'attachment',
'post_parent' => $post->ID,
'numberposts' => 1

);

$attachments = get_posts($args);

if ($attachments) {
foreach ($attachments as $attachment) {
the_attachment_link($attachment->ID, false);//display the image
echo apply_filters('the_title', $attachment->post_excerpt);//display the caption
}
}
}


after while (have_posts()) : the_post();

2011-01-10

Rashad Aliyev answers:

Please read here: [[LINK href="http://codex.wordpress.org/Function_Reference/the_post_thumbnail"]]http://codex.wordpress.org/Function_Reference/the_post_thumbnail[[/LINK]]


Cartogram comments:

My apologies, I will be more specific. I am looking to display the featured image and then display the caption for that featured image below it.

I do not think the link to the wordpress codex explains how to do that. Thanks anyways.

Matt

2011-01-10

hemant hapani answers:

hi....

Pls try this code...

<?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;
?>


Cartogram comments:

I could not get this to work, how would I implement it into the following template...


get_header(); ?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<div class="wrap-inner" id="post-<?php the_ID(); ?>">
<?php echo get_the_post_thumbnail($page->ID, 'full'); ?>
</div>

<div id="page-content" class="wrap">
<div class="wrap-inner">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
</div>
</div><!-- .entry-content -->
</div><!-- #post-## -->


<?php endwhile; ?>
<?php get_footer(); ?>