Since version 2.9.2, the post thumbnail feature has been supported by WordPress.
A couple of days ago I was working on a front page layout that has rows of post thumbnails in three columns. Clicking on the thumbnail takes you to the full post for the page.
I didn’t want a title, date, posted by or any of the other usual post content – I simply wanted the thumbnail. I realized, however, that additional information about the thumbnail would be useful. I thought captions, like the ones used for images within posts, would be the solution but discovered that they didn’t work with just the code for the thumbnails. So, I set out to see if there was a solution.
The code used to display thumbnails on a WordPress template file is
<?php the_post_thumbnail(); ?>
After searching around for a bit I located an article by Bill Erickson called WordPress Post Thumbnails with Captions that had the solution.
The image is stored as it’s own post in the WordPress database and the caption is stored in the post excerpt. To pull the caption with the thumbnail the following piece of code is added after the post thumbnail code.
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
The full block of code is:
<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
I didn’t find any other documentation on this other than Bill’s article but it added the caption. Problem solved.
Of course, the caption will be floating around next to the image so you will need to add styling to it so it is placed in the correct location.
photo credit: nocklebeast
Colleen says
Nice investigative work Kim. :)
Kim Woodbridge says
Thanks Colleen!
Dennis Edell @ Direct Sales Marketing says
Where is this thumbnail feature?
Can you show a site example also?
Kim Woodbridge says
Hi Dennis – On the right side of the edit post/page screen is a link that says ‘Set Featured Image’. This is where the image that will be used as a thumbnail is added. The code to display it is in the template file.
I don’t know of any sites using it although I’m sure some are. The example from my screenshot is from a site that isn’t live yet.
Dennis Edell @ Direct Sales Marketing says
Ah, that explains why I’ve never seen the link, I do not have the code…how is one to know of it’s existence without reading a post like this?
Kim Woodbridge says
Support for post thumbnails was added in 2.9.2. If you are running that version + WordPress supports it. The issue, however, is that our existing themes don’t contain the code for them. I had to add the code to this theme. If you are install version 3.0 and use the new 2010 theme, it is already built in.
I wrote an article about adding the code last Spring
http://www.kimwoodbridge.com/how-to-add-the-post-thumbnail-feature-to-your-wordpress-installation/
I’m not sure how people would know if they don’t follow WordPress news and version updates. Or read this site ;-)
agabu says
Thank you Kim. I’ve read some articles in that subject but what I didn’t read anywhere was the important thing you wrote:
“The image is stored as it’s own post in the WordPress database and the caption is stored in the post excerpt.” Thank you for pointing that. Now I see the clue.
Kim Woodbridge says
Hi – Glad it helped!
Neil says
Hi Kim,
I’ve found that it is quite easy to have captions (I just write the words in the box marked ‘caption’ under the image in the media library), but I can’t figure out how to format the caption.
The caption is always in italics for me, but I would like it to be normal and have the option to add style and even link out to the image source.
Do you know the trick to un-italicize the captions?
Kim Woodbridge says
Hi Neil – This article might help. I have not, however, used the styling with the post thumbnails so I’m not sure how it will work.
http://designm.ag/tutorials/wordpress-caption/
Neil says
Wow… erm that’s a little out of my league :-) I think I’ll leave it for now :D