Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function to get the caption for an image in wordpress

I'm trying to get the captions from images in Wordpress but I can't find an easy function to grab this bit of information.

Anyone know a way to get this?

Thanks

like image 944
Paul Sheldrake Avatar asked Oct 23 '09 22:10

Paul Sheldrake


1 Answers

If you are trying to get the caption while in a post you can echo it out inside your "the_post_thumbnail" tag.

<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>

You can also use the same method to show the image description. This is a little better feature in WordPress 3.5

<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_content; ?>

If you need to style the caption or description you can wrap it in a div like below.

<?php the_post_thumbnail();
    echo '<div class="myDiv">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>'
; ?>

Hope that helps.

like image 194
unscripted Avatar answered Sep 28 '22 08:09

unscripted