Before the_post_thumbnail was available, here is how I would fetch an array of images that were attached to a post in wordpress:
<?php $images = &get_children('post_type=attachment&post_mime_type=image&orderby=menu_order&order=ASC&post_parent='.$post->ID); if ($images): $image = current($images); $imageData = wp_get_attachment_image_src($image->ID); ?> <img src="<?php echo $imageData[0] ?>" alt="<?php the_title() ?>" /> <?php endif ?> <!-- image title: $image->post_title, caption: $image->post_excerpt, description: $image->post_content -->
Usually we’d use this just to pull out the first image attachment to a post and use this in a special place in our template. Now that there is built-in post thumbnail support, we wanted to use this feature but also still have the ability to display the selected thumbnail’s caption information. the_post_thumbnail only returns the image tag, so here is our custom filter (inside our template’s functions.php file) that adds our caption:
function monahans_thumbnail_caption($html, $post_id, $post_thumbnail_id, $size, $attr) { $attachment =& get_post($post_thumbnail_id); // post_title => image title // post_excerpt => image caption // post_content => image description if ($attachment->post_excerpt || $attachment->post_content) { $html .= '<p class="thumbcaption">'; if ($attachment->post_excerpt) { $html .= '<span class="captitle">'.$attachment->post_excerpt.'</span> '; } $html .= $attachment->post_content.'</p>'; } return $html; } add_action('post_thumbnail_html', 'monahans_thumbnail_caption', null, 5);
The result of this filter is now anytime we call the_post_thumbnail in our template, the html will come back with our image caption, too.




Hello,
Wonderful plugin, thanks. I spent a while looking for some way of displaying my captions.
I also wanted to use the caption as a rollover state of a clickable thumbnail. I think this requires some code tweaks. Do you think this would even be possible?
I could send the code in my functions file for my loop if that helps.
Cheers
Rob
By Rob on Mar 27, 2010
By Rob on Mar 27, 2010
Hey great function, but when I use it, the more tag no longer functions on the page, also my first article without a feature image gets no title displaying. Any ideas?
By Bill Mead on Oct 11, 2010
Thanks for the code. In principle it works, but the caption is shown inside the a-tags and therefore it smashes the design.
Any hints for me, how to put the caption outside the a-tags just below the thumbnail with the same css tags used in the single article?
By Hansjörg on Nov 22, 2010