Add Thumbnails to Wordpress with Custom Fields
In this tutorial, I’ll show you how you can easily add thumbnails, lead image, and other extras to individual posts in Wordpress. These images can be shown on the front page, archives, search pages, etc - but appear outside of the content, giving you full control of their placement and style.
What Are Custom Fields?
If you’re reading through this article, there’s a possibility you’ve never heard of Custom Fields (or maybe you just haven’t seen a use for them yet). Essentially, all a Custom Field does is allow you to add extra pieces of data to individual Wordpress posts that otherwise aren’t there by default. In other words, along with things like the Post Title, Entry, and Categories, you could add:
- Thumbnail
- Lead Image
- Price (perhaps each of your posts is a product that you need a price label for)
- Source Link
Sure - but you COULD also just put those in the post… right?
Of course, but when you do that, you’re stuck with your default posting format and styling. What if you want to have a Thumbnail for your post, but instead of the thumbnail appearing inside of the post entry, you only want that thumbnail to appear on the front page of your blog - and using a completely different style than it would in your entry?
This is where Custom Fields come into play. They allow you to add as many additional fields as you like, all of which can be used anyway you want to use them.
Adding a Custom Field
The first step to making custom fields work is to actually add one to your Wordpress Blog. To do this, simply start making a New Post, and scroll down the page until you see “Advanced Options“. Under this heading, you should see something like, Excerpt, Trackback, and Custom Fields.

To add a Custom Field for a Thumbnail, just fill in the Key with “Thumbnail”. For the Value, post the full URL of the thumbnail you would like to use.
Click Add Custom Field, and it will apply to that post.
Creating Thumbnails for your Posts
Now that you have a key called Thumbnail, you’ll never have to create that key again. It’s stored so you may select it from the dropdown box for now on.
In each post that you’d like to have a thumbnail, you’ll have to go to the Custom Fields area of your Write Post page, select “Thumbnail”, and fill in a value with the path to the thumbnail you’d like to use.
Now, let’s insert this thumbnail into your post listings on the front page.
In your theme files (I am assuming that you have a basic understanding of modifying themes in Wordpress), open up index.php. Find these lines:
<?php while (have_posts()) : the_post(); ?> ... <?php endwhile; ?>
The code between these lines is the output for each individual entry shown on the front page of your Blog. So, let’s say you limit your blog to 10 posts per page, this is the code the repeats 10 times to display those 10 posts.
Within these lines, we want to add the following code in order to show our newly created thumbnails:
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" />
This outputs an image, using the Value of the Key, Thumbnail, which we’ve inserted into our post. It can be inserted ANYWHERE within the while loop, allowing you to float it to the left or right of the header and entry, display it above an entry, below, whatever you want.
Great, but I want this to show up In the Individual Post Too
That’s possible as well, using the same code as shown above. You just need to make sure you insert the code that displays your thumbnail in the while loop.
Maybe you want to display a different image on your post page, such as a larger or smaller version of your thumbnail. At Tutorial9, we often times have Lead Images at the top of our posts which are much larger than our thumbnails. This is very easy to achieve as well.
On your Write Post page, add another Key to your Custom Fields called “Lead Image“, with a value pointing to the URL of your lead image.
Now, in your single.php theme file, insert the following code inside of the loop:
<img src="<?php echo get_post_meta($post->ID, "Lead Image", true);?>" />
Simple!
Only Show a Thumbnail or Lead Image on Some Posts
Maybe some of your posts don’t require thumbnails or lead images. We don’t want to output unnecessary code… With a few additional lines, we can fix this problem.
First off, we’ll want to put this right after the loop starts:
<?php $Thumbnail = get_post_meta($post->ID, 'Thumbnail', $single = true); ?>
This won’t output anything in HTML, but will store the value of our Thumbnail field in a variable called $Thumbnail. If the field was left empty during the post creation process, this variable will also be empty.
Next, we need to modify our output a bit:
<?php if($Thumbnail !== '') { ?>
<!-- Any special styling you might have for posts with thumbnails... -->
<img src="<?php echo $Thumbnail;?>" />
<?php } ?>
What this does, is checks to see if the Thumbnail variable is empty. If it isn’t empty, the code will output the thumbnail image. However, if it is, nothing will be shown here.
After you’re finished modifying your code, upload, and test to make sure thumbnails are being displayed properly.
Now What?
You’ve reached the end of this post. Seeing you made it this far means you might be interested in these related articles and resources:
Free Goodies. Delivered to You.
Subscribe to Tutorial9, and we'll deliver you the newest freebies and tutorials for free.
Subscribe By Email Subscribe By RSSWith the help of these structured lessons, you'll be a master of Photoshop in no time!
Most Popular Posts
Write for Tutorial9
- Do you want to get paid $150 for writing at Tutorial9?
- Are you a talented Photoshopper, Blogger, or Photographer?
- Want to help thousands of others by sharing your knowledge?
If so, we're interested in you, and we'll pay you. Find out how to write for Tutorial9.




91 Comments Leave a Comment
Am I wrong to say that these won’t show up in RSS feeds?
That’s correct - you’ll need to edit your RSS output in order to show custom fields in your RSS feed (like we do here at Tutorial9).
exactly the type of thing i need, thanks mr legget
Thank you muchly for this!! Exactly what I was looking for.
One question…how can I get the thumbnail to be above my text instead of showing up below it?
(I’m a newb - can’t figure it out!)
Eu precisava disso, muito obrigado
pt-br
Great post and easy to understand! I just added this code to the upcoming post query in the sidebar on one of my sites nex to the_title() and it looks niiiiiiiiiice. Thanks.
Is there a way to say… if there is no Thumbnail is specified, display placeholder.jpg ?
I have done all the steps listed an the image is not showing up. My image is here: http://pitchersdrills.com/wp-content/themes/church_30/images/thumbnails/mirrorthumbnail.jpg
I added the custom fields “thumbnail” and the link to the image shown above
I also edited the index.php coding you suggested
1. <img src=”ID, “Thumbnail”, true);?>” />
directly underneath the : #
“This outputs an image, using the Value of the Key, Thumbnail, which we’ve inserted into our post. It can be inserted ANYWHERE within the while loop, allowing you to float it to the left or right of the header and entry, display it above an entry, below, whatever you want.”
As you can see on my homepage pitchersdrills.com the thumbnail is not showing up under the “TOOLS” section of my homepage “read more” area.
Thumbnail goes with the paragraph below…like they have in the demo on the church 30 wordpress template: http://www.studiopress.com/demo/church.html
“The mirror is one of the most basic pitching aids available to athletes. Mirrors are great for practice since you can make sure you are in the correct pitching positions at all times. Unlike cameras and video-recorders, mirrors don’t require batteries and can be used to practice on your own. You also receive clear and instantaneous feedback with every practice drill you do. Mirrors can be placed on the ground at an angle or mounted on the wall with clips and screws. The mirror pictured below… [Read more of this review]”
Thoughts?
It’s hard to say without actually having access to the blog. My first guess is that the name of the custom field, and then name typed in the theme code are not an exact match. Be sure both of them are “Thumbnail”.
Also, be sure you’re actually clicking “Add” or “Update” when inserting values into custom fields.
Do you have a URL where we can see it?
Great tutorial thanks
Okay here is a screenshot of what I am talking about.
http://pitchersdrills.com/wp-content/themes/church_30/images/thumbnails/pagecoding.jpg
cal, my guess is that you’re editing the wrong part of the template. That said, I think there is actually an alternative solution in your case (this does not apply to anyone else).
It appears that your theme actually includes an easy way to use thumbnails by default (without manually adding code as this tutorial suggests). Do me a favor, and change the title of your custom field to “hpbottom” for the thumbnail. Make sure the image size is 70×70.
There is a note in your HTML that suggests this should work for the theme you currently have applied to your blog.
wow you are the man - woot woot!
Great tut if you want something unique! But let me one tip
If you just want to add a thumbnail to your post so it can be identified in a visual way too (and of course, to attract visitors), there are WP plugins for that and no coding is needed.
I’ve done the steps to create a lead image, and on refresh it looks like it *wants* to load the image, but then I get the “broken/missing” image, quickly replaced with the alt text. I’ve tried giving the full path to the image and that doesn’t work either. Note: I’m running a modified Sharpfolio 2 theme.
Any ideas? I can send screenshots or whatever…it’s driving me nuts.
Thx.
Could you link to your site?
Hey another related question. Maybe you have already covered this on the site…
How do you get a tiny thumbnail to post to the left of the post title?
You have “Add Thumbnails to Wordpress with Custom Fields” as the title for this post - how could you add say a wordpress icon to the left of that phrase?
Thanks
For certain phrases, it would take a considerable amount of PHP work (at least, the way I’d think of doing it). You might be able to do it easier by assigning them to specific categories or tags, and then use if/or statements to get the icons to display.
Thanks for great tutorial.
I need some kind advices from u with regards to my problem.
Actually i’m currently using ‘Malawak’ theme (free Download) and it working fine on my blog (http://myteames.com/blog).
However, i don’t know why thumbnail image did not appear.
I’ve added Custom Field and URL of image, but still no image shown on my page.
Can u have a look what’s actually the problem and advice me?
Thanks in advance.
You appear to be running a plugin that generates thumbnails. I’m not sure how that plugin works unfortunately
This makes a lot more sense than some of the other guides on this.
I know that index.php controls the master page(s) but I am not sure on the individual posts. Are you supposed to hook into single.php or page.php?
Don’t worry, its single.php right.
Hey,
I would be happy with the SAME icon appearing next to all the titles. This guy has the concept down http://shuckabuck.com
He has a poker chip with a mini date inside the poker chip. Looks pretty clean - I emailed him about it but no response.
Do you have any ideas???
-cal
Hello, thanks for the nice post. Now i have a problem here. I want to show attached image only in frontpage post.
So when I add a new article to my blog I add a main image to every post, of course the image also will display when viewing a single the post.
What i want is to show the attached image only on the main page post and not on a single post.
Please tell me the way.
I have tried everything you said, But can’t get it work
My index is called home.php
I tried customs, and added exact image location
I tried thumb and thumbnail for name
is what my code looks like
site is http://www.twittermugshot.com
http://i83.photobucket.com/albums/j317/jubril58/Capturecode.jpg
I know this free but I can i get response please. I would really appreciate. I love this site its put together well and very helpful. This I need to find answer too
Thanks for this tutorial. Any way to amend this code so that I can show an image next to recent posts in the sidebar?
how to show different thumbnails for different categories ? ? ? ? ?
see how I implement it on my site! It works great !!! thanks… -) It’s worth to see!
You can also accomplish having a thumbnail image on the index and regular image on the single post page by using the excerpt and content hooks accordingly.
For example, if you wanted to show a thumbnail when viewing a list of posts you can add the code to the excerpt and a short description. Then in index.php change the_content() to the_excerpt() and I believe it will work with RSS too.
great topic …
i made it completely.
thanks
Thank you so much for this great tutorial , and its so easy
I just want to ask you please, can I make a custom-filed and make it for audio files . ex: mp3 to the post without any mp3 or audio plugins?
sorry for my bad English
Thank you again and god bless you .
regards
Custom fields are just a convenient way of adding any extra data you want to a specific post. How you use the data is completely up to you—it just requires you to do some configuration on your end.
For example, you could upload an MP3, have an “mp3″ custom field, and then use an if statement in your template file that detects if there is an Mp3 file associated with the post (and if so, to add a player of sorts that calls that specific file).
The possibilities are endless, it’s totally up to you how you use the data in custom fields
thanks for the tutorial.
thank you very much!
can i add a function to that,
that will tell him to put a specific photo,
if there isn’t a thumbnail to the post?
Hey…
Nice blog!!!
I tried following your steps to get my thumbnails in my index page, but it didn’t seem to want to work.
I have the theme called “WP-Magnum Black”, I was looking around the code in the index.php to see if this theme has included an option to play around with thumbnails, but my knowledge in programming is limited. I was wondering you if you could help me out.
I want the thumbnails to be part of the preview of the posts that appear at the front page of my blog.
Thanks for everything, before hand.
JP
What code do you use to add those beautiful related post thumbs under each of your blog posts?