![]()
WordPress magazine style themes have been all the rage lately, and everyone seems to want some aspect of their WordPress blog to have a magazine-y look and feel.
So if you have been around blogosphere recently, you must have seen those cool image thumbnails with short excerpts displayed on many blog home pages.
Today I went around searching for the easiest post thumbnail method, and was surprised to see how unnecessarily complicated some of the code junkies make something as simple as adding image thumbnails to posts.
So I hunt around a bit more and came up with an easy as pie way to add pretty little thumbnails to a WordPress 2.5 powered blog.
What you need to know: You need to know how to copy and paste.
What you don’t need to know: You don’t need to know how to open Photoshop.
So let’s get started.
WordPress 2.5 has a very handy new feature. It lets you specify custom thumbnail size for your uploaded images.
To adjust the thumbnail size to your liking, go to Settings > Miscellaneous and change the thumbnail width and height under Image sizes section. Let’s set the width and height to 80×80 for this example.
Also, make sure the Crop to size option is selected, and confirm the changes.
(With Crop to size checked, you don’t have to worry about what size your original image is. It will be adjusted to fit your specified size)
We want our thumbnail to be aligned to the left while the text flows around it. To do so, add the following code to your style.css file:
.thumb {float: left;margin-right: 7px;margin-bottom: 7px;}(Feel free to change or add to the code)
Next, we need to modify some code in index.php to correctly display the post thumbnails.
In your index.php, locate the part that is responsible for displaying your post content. It must be the following line or something similar to this depending on which theme you are using:
<?php the_content(); ?>Replace it with the following snippet:
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php $values = get_post_custom_values("thumb"); echo $values[0]; ?>" alt="<?php the_title(); ?>" class="thumb" /></a><?php the_excerpt(); ?>(This code snippet retrieves the custom field value that we’ll add in the next step)
Now that everything else is done, it’s time to add the image.
Done! Write the post as usual, and don’t forget to enter a few lines in the excerpt field to be shown next the thumbnail.
Do you want to display regular content in case there’s no thumbnail image? No worries. David has come up with a code snippet that does just that for you.
<?php$thumb = get_post_meta($post->ID, "thumb", true);if ($thumb != ""){ ?><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php $values = get_post_custom_values("thumb"); echo$values[0]; ?>" alt="<?php the_title(); ?>" class="thumb" /></a><?php the_excerpt(); ?><?php } else { ?><?php the_content(); ?><?php } ?>Read on David’s post for more detail.
If I missed an important detail, or if you know of a better, easier way to add thumbnails to posts, let me know about it in the comments.
Thanks for sharing the tip - if you’re looking to make the thumbnails a regular feature, you might as well go with a magazine theme IMHO.
Also, adding a more quick-tag just below the image also helps in giving a thumbnail like effect.
Karthik, sure there is no dearth of WordPress magazine themes, but if someone is looking to pimp up their old theme, they’ll have do it manually.
Hm, I didn’t even notice this feature in 2.5. No more late night surfing for me…
I use Post-thumb revisited plugin (http://www.alakhnor.com/post-thumb/) to display thumbnails in 2.3.3. Quite good because it will automatically generate a small thumbnail of the first image in your post based on your configuration.
Jeremy, the first thing I did on WP 2.5 was explore the settings and hunt for new features. Almost all ideas voted by WordPress community including one click plugin updates and thumbnail resizing are there in the form of new features.
Kevin, thanks, I’ll have a look at the plugin. Though, are you sure this plugin gives you as much control over thumbnails as the manual method?
I have upgraded my blog to the latest WP 2.5 and yes it has far more useful features than the previous 2.3.3. I think I’m going to use your method for showing thumbnails.
But what do I need to add in the code snippet if I don’t want to show the thumbnail particularly in that post?
Great tutorial, I’ll implement something using this feature as soon as the maintainers of the plugins I use update their code to suit WP 2.5!
I’m in the process of creating a theme from scratch for a client and was looking for a simple way to create thumbnails on the index page as he didn’t want other bells and whistles that entailed a magazine-like theme.
I take what I said earlier back, this is an awesome method that is so simple and effective - thanks!
Just a pointer though - the Image Sizes section is under Miscellaneous and not Writing, not sure if it was changed when WP 2.5 became stable or not.
I’m just going to test it out, will let you guys know how it went, I’m sure its going to work like a charm!
Kevin, not sure about this because the custom field data is called directly from the index file so it doesn’t leave any room for overriding the code for individual posts.
You could omit uploading the thumbnail image for a particular post, but it would still leave an empty square where the thumbnail would have been.
Though you could write a conditional statement like this to tell the WP to show nothing in case the ‘thumb’ custom field is empty:
if (image exists) {
show image
} else (show nothing)
You can convert this pseudo-code into real PHP code, but I’m sorry I can’t be of any help here. My knowledge of PHP is next to nothing.
Copes, I’m glad you like it.
Karthik, Do let us know if it works for you.
Yes, it seems they have changed the location of resizing options in the stable release. I’ve corrected it now. Thanks.
Kevin,
If you need more pointers regarding implementing it, please contact me at karthik at iamkarthik dot com as the comment box doesn’t accomodate html and php code.
Mohsin,
I totally love the theme - would you mind if I made a few changes to the theme to make it WP 2.5 compatible? On a similar note, was the avoidance to support tags a conscious decision (I plan to include it if it were an accidental omission) ?
Karthik, is it Blue Ecstasy you are talking about? If so, sure do whatever you want to do with it.
The thought of adding tags support crossed my mind once, but then I decided to keep it as basic as possible.
But I might add tags support in an update later.
I wanted to use this feature on a project, but only wanted to use thumbnails for posts on occasion.
Using your code I seemed to have pulled off a statement to check for the thumb, and if no thumb exists it defaults back to the original formatting.
I barely know PHP, but it works on my site, so take it for what’s worth, or feel free to fix it up.
ID, “thumb”, true);
if ($thumb != “”)
{ ?>
<a href=”" title=”">
<img src=”" alt=”" class=”thumb” />
Ok… So I guess code gets stripped in the comments.
Using this code above as a base, I added an argument to allow a bit more flexibilty. The argument checks if a thumb is assigned to the post, and if not, the original code is used instead of the thumbnail code as to avoid the empty placeholder for where the thumb should be.
Hope it helps!
http://www.widecastmarketing.com/thumbnail-for-your-wordpress-2.5-posts.html
I don’t use wordpress myself because it lacks advanced features that I want but inserting thumbnails into WP posts is something that I figured out how to do with WP when I tried it out. Its actually very easy.
Create a new post and on the editor page upload the image using the FILE upload feature below the WYSIWYG editor. Once the image is upload you are given a chance to display it as a thumbnail or full sized image. After you select “thumbnail” you click on “send to editor” and then it will display the thumbnail in your post. If you want to left align the image you can add a custom css div using the code view of the editor.
Or you could use serendipity (www.s9y.org) and you’ll get a full featured media manager and the ability to upload and insert right, left or center aligned thumbnails/full sized images straight out of the box :).
Thanks, David. That works!
does it work in any themes ?
Great post. Thanks for this wonderful tutorial.
abdussamad, well that’s obvious, but we are talking about displaying post thumbnails only on the home page.
Bill, there is no reason why it shouldn’t!
Thank for your tip.
Thumbnail works great but it has made the text drop below. http://www.onlythegames.com how do I fix this?
Erik, it has something to do with the theme you are using. Try clearing the float and see if it fixes it.
Hi,
Firstly thanks for this excellent article… I have been searching for something like this for ages.
What I want to do basically is have a list of posts with thumbnails from a single category (like in your example) display in my sidebar. Do you know If I would be able to do this with this code? I’ve been searching for ages for some way to do this, and like you mentioned I found post thumbs revisited just way too complicated for my needs. I havnt been able to find any other plugins etc ,or ways to do this..
Many Thanks
Martin
Martin, try this code and let me know if it works for you.
<?php query_posts('cat=4'); ?><?php while (have_posts()) : the_post(); ?><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php $values = get_post_custom_values("thumb"); echo $values[0]; ?>" alt="<?php the_title(); ?>" class="thumb" /></a><?php endwhile; ?>Change
catid to your desired category.Thank you for this post. I was a little lost trying to add thumbnails with WordPress 2.5. In WordPress 2.3.3 my thumbnails were by default being sized proportionately. With your post, I learned that the default thumbnail size in the WordPress Upgrade is set to a standard 150*150. All I had to do was deselect the fixed size setting to get back to what I’m used to from 2.3.3. I’m glad to have come across your blog. I’ve added it to my Feed. Thanks.
I still don’t like the limitations of WP thumbnails. You only get two options. I found the Viva plugin from meditricks works great for magazine style themes. It’s a paid plugin but was worth the few bucks as Tim and the guys helped me set it up and there support Rocks! What I love is the way I can add multiple styles of thumbnail to my homepage just by uploading with a post. Seriously worth checking out if, like me, you are playing with a move to a more magazine style theme. (Link is http://www.mediatricks.biz/demo) Tell ‘em I sent you - you might get discount!
Thanks a million, I modified it a little to suit my needs and it works perfectly.
This wordpress modifying is getting easier by the day thanks to great sites like this.
Hey, I was just about to write something kind of similar to what David’s update showed, so was looking around for how other people did it, and arrived here. My question is about your use of
Why is the thumb variable not used? How does get_post_custom_values differ?
Alex
Oops, that should have been as line 6 to 7 of the update
Alex, that’s because this particular part was copied as is from the original code of mine.
If you have any questions regarding David’s code, please post them on David’s blog as he’ll be better able to explain what he’s done.
Thank you, thank you! That took all of five minutes (and here I thought it would take all night!)
this is a great solution.
any idea how it can be used to display thumbs as related posts? (Post-thumb revisited plugin doesn’t work for me).
letrodectus, I really think it’s possible to achieve this functionality by modifying an existing related-posts plugin, but since I’m not a coder, I’ve no clue how to do it. You may want to contact a WordPress Plugin coder and request him to do that for you.
hmmmm i tried it last time with 2.5 and working smoothly but after upgrading to 2.5.1 it doesnt work