solostream

Giving each WordPress post a thumbnail, and display the thumbnail on the home page

Miriam Schwab | November 21, 2007 | 169 Comments

You may encounter situations where you need to automatically display a thumbnail or image of some kind that will link to posts. Here’s an example of this kind of situation: you want to display recent posts from a certain category on your home page, with a thumbnail for each post and maybe the title and an excerpt. Or, I recently had a client that had a box on their homepage for multimedia where they wanted an image to appear that would represent the latest post in the Media category and would link to that post. A link to this site is at the end of this post, so read on.

I found the solution in two amazing WordPress themes: Mimbo and The Morning After. They both use thumbnails on their home page, and they do it by using WordPress’ mysterious Custom Fields feature.

You need to be able to upload images to your server in order to do this. You can do this via FTP, but to make this as easy as possible for clients, I use the Filosofo Old-Style Upload Plugin which creates a link on your nav bar where you select files for uploading. But first you need to configure this plugin and select a folder where all uploads will be saved:

  1. Upload the plugin and activate it.
  2. Go to Options > Uploads.
  3. Enter your destination directory. I think the best is to use your images folder in your theme folder so that any images you upload will be in an easily portable place. So the path could be something like /home/server/public_html/wp-content/themes/themefolder/images. The plugin will suggest a path, and it is usually right.
  4. Enter the URI of the folder. It’s something like http://www.yoursite.com/wp-content/themes/themefolder/images.
  5. You might as well up your maximum file size. I make it 500 kb.
  6. Allowed file extensions: jpg, jpeg, gif, png, pdf – I add PDF and other types of files I think clients may want to upload.
  7. Minimum level to upload: I leave it at 6 since I have no idea what this means.
  8. Click Update Options.

That’s done. Now for how to create a post with a thumbnail image.

  1. Create an image for your post with the right width and height. The size of the image depends only on the design of your site and where it is going to appear.
  2. Go into the admin of your site. Click on Upload (it’s on the upper nav bar).
  3. Select the file you are uploading, select No Thanks so it won’t create a thumbnail, give it a description (that’s good for Google) and click Upload File.
  4. Once it’s uploaded it will show you the entire URL of the image. Note particularly the last part of the link where it says the name of your image, especially if there are any capitals in any part of the name. You will have to enter the exact file name in step 8.
  5. Go to Create Posts. Enter your post and title as you wish. Make sure to select the category you have selected as the one that will appear with the thumbnails. Now scroll all the way down to the bottom of the screen where it says Custom Fields. Click on the plus button to expand it.
  6. Enter the word Image (with a capital I) in the Key field.
  7. In value, enter the exact name of the file you uploaded. For example picture.jpg or image.gif. Click Add Custom Field.
  8. Save your post.

Now we need the code that will make this thumbnail appear where you want it to appear. Following is the code for having a thumbnail appear with the title underneath it. This code is adapted from the Mimbo theme, but the code in The Morning After is similar:

<?php
// this is where the Features module begins
query_posts('showposts=1&cat=199'); ?> //change the showposts number to the number of posts that you want to appear, and change cat=199 to your category number, which you can find out by going to Manage > Categories
<?php while (have_posts()) : the_post(); ?>
<div class="thumbnails"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php echo get_option('home'); ?>/wp-content/themes/themefolder/images/<?php
// this is where the custom field prints images for each Feature
$values = get_post_custom_values("Image"); echo $values[0]; ?>" alt="" /></a>
<p class="title"><a href="<?php the_permalink() ?>" rel="bookmark">
<?php
// this is where title of the Feature gets printed
the_title(); ?></a></p></div>
<?php endwhile; ?>

Wanna see an example? Check out this site, scroll to the bottom and look at the left-most column that is called Media. That image there is a thumbnail that was uploaded and entered in the Custom Field of the post it links to.

Update Nov. 27, 2007: This post got a lot of comments here, and on Weblog Tools Collection who referred to it. Lots of people gave more recommendations on how to manage thumbnails with custom fields and/or plugins, so I collected these tips into one post which you can see here: Images, thumbnails and custom fields in WordPress.

Tags: , , ,

Category: Code Snippets, Tips

About Miriam Schwab: View author profile.

Elegant WP Themes

Comments (169)

Trackback URL | Comments RSS Feed

  1. FAQPAL Blog says:

    [...] Source… [...]

  2. Sam Rohn says:

    try the timthumbs script, see here for wordpress integration
    timthumbs lets you call any image + parameters to generate a thumbnail, & seems to be the easiest way to add thumbs to your wp site these days -
    see my site for an example, all of the thumbs for panorama previews etc are created w/ timthumbs
    sam
     

  3. [...] Resalta los comentarios de los autores [...]

  4. nana says:

    Hi,  Thanks for the info.
    When I want to “Update Option” under “Settings – Uploads”, I have error msg : “You do not have sufficient permissions to access this page.”?
    May I know how to solve the problem?
    Thanks!

  5. [...] websites to help attract traffic to these posts by the use of images. WordPressgarage has an interesting tutorial on creating thumbnails for each post on your blog and using Custom Fields to add and display them on [...]

  6. Sushant says:

    waiting for a stable wordpress version which supports thumbnails in core.

  7. Ryan says:

    @Sushant – It should be available within the next week hopefully :)
    Or you can download the 2.9 RC1 right now … http://wordpress.org/development/2009/12/2-9-rc1/
     

  8. eONs says:

    The version 2.9.1 has release.Hope it meet what you want and required. :)

  9. Ed says:

    I just downloaded the plugin and get the error “You do not have sufficient permissions to access this page.” has there been a fix for this? I am using WordPress v2.9.1.

    Thanks
    Ed

  10. Cool! ive been searching this for a week! thanks!

  11. [...] Giving each WordPress post a thumbnail, and display the thumbnail on the home page [...]

  12. joudie kalla says:

    Is there something like this for blogger? I am trying to get a thumbnail only with link to relevant post instead of thumbnail with text. It just looks s omuch better and i am going crazy to try and find it. Something like on this website i came across called http://www.noobcook.com. Can you let me know how i can do this for blogger.
    Thank you

  13. great and good for wordpress i learn more tip at this site

  14. Meru Mir says:

    Thank you very much for this article. I just have installed WP on my site and want to customize it, but it is very hard to me to understand all this php related stuff. So big thanks from a dummie! (:

  15. [...] How To Display The Thumbnail For Each Post On Homepage [...]

  16. Hi Miriam, you have very impressive blog, I really inspiring from your blog post. Your bookmarking Icons section pop-up totally outstanding. Could you please help me out about this? How can i make one like this?  Are you using any plugins  for this? And also for email subscription top right corner.  You can reply on my email.  

  17. velo says:

    great idea, i had implement in my site.
    Thank you very much

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.