Blogthority
Authority Blogging Concepts and Services

How To Add Image Thumbnail Using Wordpress Custom Fields

By Pinyo • Jul 31st, 2008 • Category: Themes and Plugins

Over the past few days, I have been toying around with the idea of setting up WordPress as the CMS for a web site directory. One thing I wanted to do with my personal finance directory is to show a thumbnail image of the featured site when I am on the main page, category pages, tag pages, and search result pages. You can see how the directory looks below:

As you know the thumbnail feature is not common in free WordPress themes, so I had to do some work to get it working. Now, let’s take a look at what we need to do to enable this feature.

How To Add Thumbnail Image To WordPress Template

1. Modify the main page (index.php) to show image

Note you could make the same changes to archive.php and search.php to get the same result. Note, I added the following code right under the title inside The Loop:

<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img style="float:left;margin: 0 10px 10px 0;border:1px solid #000;height:90px" src="/wp-content/uploads<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>” alt=”thumbnail” height=”90″ /></a>

There are two parts to this code, the <a> tag and the <img> tag.

  • The <a> tag is the same as the one on the post title. I am adding it here so that users can click on the image to get to the full post.
  • The <img> tag has a couple of components:
    • The style attribute tells the browser to float the image to left, add some white space around the image, surround it with a black border, and make it 90 pixels high.
    • The src attribute tells the server to grab the image from /wp-content/uploads directory with the rest of path and filename information coming from a custom field called “Image”

2. Add custom field called “Image” to each post

Now, when you are writing a post, you need to add a custom field called Image to the post with a value that reflects the rest of image path and filename information. In this example, we will use the same image that is shown inside the post itself. To do this:

  • Upload an image to the post
  • Switch to HTML mode and take note of the image path (see image below) — note the image path is /wp-content/uploads/2008/07/annualcreditreport.png in this example.

  • Next add the custom field called Image. Note that we already defined some of the path information in the template — i.e., /wp-content/uploads. So you are adding the rest of the path information and filename in the value field — i.e., /2008/07/annualcreditreport.png. (see image below)

  • Save and you’re done!

That’s it! Your blog should start showing thumbnail image next to each post once you completed these two steps.

Things to consider

Right now, if the Image custom field is not defined, the template will show missing image error. With a bit of PHP programming using if-then statement to detect the if the custom field is defined or not, you could show the thumbnail only when the Image custom field is defined.

This article was featured in:

Did you like this article? If so, you can get all the latest articles delivered to your email inbox for free each morning. Click here to subscribe by email. Your email will only be used to deliver this once-daily subscription and you can unsubscribe at any time.

Share this article

  • Submit to Stumble Upon
  • Submit to Reddit
  • Submit to Delicious
  • Submit to Digg

Related Articles

Related Tags

, , , ,

5 Comments

  1. gravatar
    Blog Newbie, 26. August 2008, 7:05

    Thank You.

    I have searched and read dozens of tutorials about how to add thumbnails. I’m not php geek so most of them made no sense to me. Then I found your tutorial, straight to the point, simply and well explained. Thanks to your tutorial I figured it out and thumbs are now working fine on my blog.

    Thanks again, blogthority is bookmarked I’ll come back next time I’m struggling.

  2. gravatar
    Pinyo, 15. September 2008, 22:53

    @Blog Newbie — I’m glad to hear it. Very cool and thank you for your feedback.

  3. gravatar
    Nugz4Life, 26. September 2008, 18:17

    why on earth would anyone want to go to all that trouble when you can just use snapshot.com???? It takes two minutes and sows a thumbnail + rss feed of recent posts so user can get even more depth about the site before even clicking.

  4. gravatar
    Pinyo, 26. September 2008, 20:03

    I don’t see what snapshot.com has anything to do with the article either. I think you missed my point.

  5. gravatar
    Raj, 31. October 2008, 21:33

    You don’t have to take this much headache for doing this. There is a plugin named “Thumbnail for Excerpts” available at http://www.cnet.ro/wordpress/thumbnailforexcerpts

    Just install and activate.

    You can see it in action on my blog.

Please share your comment:

Please read our comment policy and guidelines.


Please do not use the name of your site or keywords.

1 blogs that link to this article:

If your trackback does not show in 24 hours, please resend to this trackback URI.

  1. WordPress Blogging Guide - September 23, 2008 « Create A Blog Guides

Blogthority is a group blog started by members of the M-Network. As a group, we have grown and learned a lot together. We learned many things about blogging, search engines optimization, and web site design along the way — since our blogs are geared toward personal finance discussions, we created Blogthority as an outlet to share these experiences with you.