How To Add Image Thumbnail Using Wordpress Custom Fields

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.

As you know the thumbnail feature is not common in free WordPress themes, so I had to do some work to get it working. You can see how the directory looks below:

How To Add Thumbnail Image To WordPress Template

Now, let’s take a look at what we need to do to enable this feature.

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.

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:

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:

Get Free Email Updates
emailLike this article? Enter your email address and click "Subscribe" to get the latest articles sent to your email for free. Your email will only be used for this daily update and you can unsubscribe anytime.

* Subscribe via RSS now

Related Tags
, , , ,
Pinyo
Pinyo is the owner of Moolanomy: personal finance blog and Blogthority, and founder of the M-Network: personal finance blogs network.

All posts by Pinyo

6 Comments

  1. gravatar
    Blog Newbie
    August 26, 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
    September 15, 2008, 22:53

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

  3. gravatar
    Nugz4Life
    September 26, 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
    September 26, 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
    October 31, 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.

  6. gravatar
    cmsdev
    March 7, 2009, 19:57

    You can do this without the meta-field workaround. Just querying the posts and get the images:

    // get image post
    $__query = “SELECT ID FROM $wpdb->posts WHERE post_parent = ‘”.$post->ID.”‘ AND post_type = ‘attachment’”;

    $atid = $wpdb->get_var($__query);

    if (wp_attachment_is_image($atid)) {
    // 2nd attr = { thumbnail, medium, full }
    $image = wp_get_attachment_image_src( $atid, ‘thumbnail’ );

    echo $image['url'];
    }

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

Recommended Articles

Browse Categories

Related Articles From Blogthority

Related Articles From Other Sites

Featured Sites

Archives By Year

Blogroll