Using custom fields to create archive thumbnails
When I launched this site, I was instantly jumped on by Ruark asking me how I achieved the thumbnails on the home & archive pages of this site. This can be done in a number of simple ways and with a bit of imagination, you can take it to a much greater level. So use this article to understand the basics and then you can decide how best to implement these methods.
You’ll need…
Achieving this thumbnail effect requires a custom field, some CSS, and ideally a screen capturing program. The custom field will capture the URL of the thumbnail image, the CSS will be used to define the exact space the thumbnail will occupy and the screen capturing program will grab the thumbnail itself.
1) Make the thumbnail
Personally, I absolutely love FastStone Capture. The full version will cost you all of $19.95 but I promise you its a purchase that’ll save you bucket loads of time. With it, you can select a marquee area of your screen and it’ll automatically save that selection as a JPG/GIF/PNG image. Of course you can use the normal print-screen feature, crop it in PhotoShop and then save it, but FastStone will do all that for you.
2) Upload it
So once you have your thumbnail image, upload it into WP. In 2.5, the ‘Add Media’ feature will upload the image and then present you with the URL to that image.
Copy the URL and then close the upload dialog. Dont insert the image or anything.
3) Save your custom field
Then create a new custom field called ‘thumb’ (or whatever you like) and copy the URL of that thumbnail image as the value of that key.
At this point, you can save your WP post and open up your code editor of choice…
4) Add the code
Open your index.php or archive.php file, find the loop and insert something like this before the_content() is called:
<div class="thumb"><img src="<? echo get_post_meta($post->ID, 'thumb', true) ?>" /></div>
The wrapping div around that image is there so we can CSS the thumbnail as necessary.
Note that you can also make the image a link to your article like this:
<div class="thumb"><a href= "<? the_permalink(); ?>" title="<? the_title(); ?>"><img src="<? echo get_post_meta($post->ID, 'thumb', true) ?>" /></a></div>
This makes simple use of the loop variables to insert the article URL and title tag of the link.
5) CSS it as necessary
Lets say that you’d like your image to be exactly 100px X 100px at all times and you’d like to float it left so the text wraps up around it to the right. Your CSS would then look like this:
.thumb { width:100px; height:100px; border:5px solid #efefef; float:left; }
NOTE: naturally you’d want the image that you capture to be more than 100×100 so that there’s no whitespace in that div…
6) Enjoy
And thats that… Hopefully that explains it adequately Ruark?
Of course you can tailor where in your loop the thumbnail will display and how large etc it is with your CSS.
Ruark
Thanks man! Going to give this a test this evo. legend!
gestroud
I just saw your post on Web Log Tools Collection. Thank you! I’ve been trying to figure this out for a few days now.
MarkB
Great! Glad you were able to find this then
Thanks gestroud.
JmacAttack
Great tutorial!
nomad-one
Hey WP Guru, we need more of you around to help us all out with our endless little tutorial needs. Thanks for this cool tut. One thing I hate about wordpress and custom field images though is that I haven’t found a great way to automatically upload and resize the images on the fly, like a plugin or something. I know there are some dynamic upload plugins, but none that work without installing complicated php graphic libraries or something, Any advice?
MarkB
So you’d like a system whereby you could upload an image and have it automatically thumbnailed to a certain size?
Well WP 2.5 has the ability to set the size at which your thumbnails are created. The bonus of WordPress 2.5 is that you have 2 thumbnail sizes available to you – small & medium.
What I’d suggest then, is keep one of those sizes for the thumbnail size that you’d like to use in your theme. Then in your admin settings, set the size that you’d like that thumbnail to be. Now when you upload an image, you can use that particular thumbnail thats now the exact size you require.
Make sense?
bkpgeral
Very Good!
Another question. Is possible to insert thumbs of one directly post for email?
Thanks!
MarkB
@bkpgeral: i’m not sure i understand your question?
bkpgeral
Ok. I need to post-by-mail with my mobile, but inserting a url of the thumbnail for each post. It´s possible??
(excuse my bad english)
MarkB
Ok i understand… I’m not sure about that – haven’t tried it myself. If you can access the custom fields via your mobile, then you shouldn’t have a problem, otherwise I don’t think it’ll be possible…
Elaine-
wow!! i have been searching for gallery plugins to do this for me for FIVE DAYS NOW… i woke up this morning and realized i was going to have to customize EACH POST somehow and i googled and two minutes later i was on your page with the solution!!!! it only took me 15 or 16 hours to modify each post tho haha… sometimes i wonder why i love wordpress so much!! anyway, thank you and i’ve spread your link around lol, thank you thank you!!!
Elaine-
hi, well now you don’t have to make and upload thumbnails coz wordpress does it for you, but i’m wondering how to control the number of thumbnails an archive page shows, because right now it’s the same as the number of posts the page shows, and since it’s a photoblog i would like to limit my posts showing, maybe even to ONE, but then i would only have one thumbnail showing on each page of my archives… suggestions? thanks!!
Daniel
What is I want to put a custom field for a link that is different from the post page? Would it be
<a href= “ID, ‘link’, true) ?>” title=”"><img src=”ID, ‘thumb’, true) ?>” />
Daniel
I guess I can’t post code…. sorry.