Using The Attached Image With Custom Post Thumbnails In WordPress
A littler earlier I noticed a tweet by @LucPestille asking whether he should use ‘post-thumbnails’ or TAI. I answered saying probably WP’s post thumbnails as they generate their own thumbnails upon upload & you can always use the Regenerate Thumbnails plugin should you need to back track on old images.
However a good point was made that You would still need to go back through (what is possibly 100’s of) posts setting a image to use as your featured image. Then I realized what if I could combine the ability to use WordPress to generate custom thumbnail sizes with the ability of The Attached Image to take the first attached image and display it. This then takes care of the problem of going through all the posts to pick a featured image. Well that’s what the recent update (version 2.6) brought to The Attached Image.
To make things easier here is a little tutorial on how to add custom post thumbnails to your theme & then use them with TAI.
Add Thumbnail Support To Theme
To add thumbnail support to your theme is really easy. Just open up your theme’s functions.php
file or if it doesn’t have one create it. Then add this, at the top is generally a good place:
1 |
add_theme_support('post-thumbnails'); |
Obviously it’s a good idea to make sure your theme doesn’t already support it, but it shouldn’t do any harm to add it in again.
Set Thumbnail Sizes
Now to get WordPress to make thumbnails the size you want add this:
1 |
add_image_size('customthumb', 200, 150, true); |
The first parameter is a unique identifier for your image (I’ll use ‘customthumb’), the second is the width, third is the height & last is if the image should be hard cropped to the size. Leave it out if you do not want cropping.
For each one of those you add WordPress will generate a thumbnail for it when you upload images, so remember that image uploading may take a little longer than normal.
If you already have images uploaded you will need to use Viper007Bond’s Regenerate Thumbnails plugin to create the new thumbnails for the images uploaded before you added post thumbnail support.
Using With The Attached Image
Normally you would use the_post_thumbnail();
at this point. However if you already have a lot of posts it will be a pain to go through setting a featured image for them. So instead you can use the latest version (2.6) of The Attached Image to show the first attached image. In most cases that should be the correct image & is hopefully a quicker alternative to going through all the posts.
To get it to work all you need to do is give the unique identifier (remember ‘customthumb’ from before) you used when setting up the thumbnail sizes to the img_size
parameter. Like this:
1 |
the_attached_image('img_size=customthumb'); |
That should grab you the correct thumbnail size. If an image doesn’t have a thumb with that identifier either due to you not having regenerated the thumbnails, or due to the image being to small to create it, it will just go blank as to not interfere with normal operation.
If the first image is not the one you want to show you can use the image_order
parameter to change which image is shown.
That’s all there is to it. In my opinion this is a much better alternative to using The Attached Image with timthumb due to the heavy load that timthumb can but on shared or low powered servers.
I hope this small tutorial has been of use to you. Let me know if you have any questions or comments let me know.
5 Comments
Tracy Lee
Hey paul- you have previously helped in the past- I’ve really tried to fix this without bugging you..
I have a 125×125 sqaure that I need to pull an image from a post to the front page of the site. It was doing it using the HEIGHT of the image so that the entire square was filled. All of a sudden, it stopped working and it’s resizing the original image to a 125 WIDTH instead, leaving an empty space below the image in the box. Please tell me i’m just being stupid and it’s an easy fix?
thank you,
Tracy Lee
Paul Robinson
Hi Tracy,
I deleted your other duplicate comment. Also apologies if you’ve had trouble getting to the site, we’ve had some really bad luck with server problems (hardware failures) over the last few days.
I’m not really sure what would cause that to happen. Could you give a link to the page, or paste the code you are using in pastebin.com?
Tracy Lee
the site is DJmegaman.com
i’m using just the very basic code that you provide.. What was weird was I was thrilled with how it looked, and then all of a sudden it changed so I wasn’t sure if something else I did changed it??
Paul Robinson
Very odd. Can you drop me an email via the contact form & I’ll take a look. It might take a few comments & it might get a little messy here, lol.
If you can paste the code you are using into a pastebin.com page & add the link in the email that would help a lot.