Dec 28, 2009

Posted by Brian
in Featured, Wordpress | 2 Comments

Post_thumbnail in WP 2.9


Wordpress introduces a new standardized way to use post thumbnails to your blog. With the heavy use of this feature from all sorts of theme or plugins it was only a matter of time that Automatic decided to come out with their integrated solution. In this post I will run you through in order to have that feature running on your site.

1) Activation

First you will need to activate this feature in order to tell WP’s UI that you are requiring this.
In your theme functions.php file you will need to add these lines:

add_theme_support('post-thumbnails');

For backwards compatibility of your theme you should wrap this inside a function check for the new add_theme_support:

if ( function_exists( 'add_theme_support' ) )
add_theme_support( 'post-thumbnails' );

2) New post/page widget

After initializing your function go to a post and check out the new “Post Thumbnail” widget available under your “Categories“. Sweet! now you can upload a picture and chose the thumbnail image size you want to use via the image uploader that you are already been using. the only change will be the “Use as thumbnail” link besides the “insert into post” button.

3)Call the thumbnail in your theme

Next step is to call your thumbnail in your template files.

the_post_thumbnail();

It’s as simple as this. There are a few other options you can use to customize the your thumbnail output as well

Here is the css output:

.wp-post-image {
border: 2px solid #ccc;
}

4) Options

Set your thumbnail to use specific size:

// the thumbnail
the_post_thumbnail(array(100,100));
// medium resolution
the_post_thumbnail(array(300,200));
// large resolution
the_post_thumbnail(array(600, 400));
// original
the_post_thumbnail();

Add your own class:

// align right and the class  'NEW_CLASS'
the_post_thumbnail(array(100,100), array('class' => 'alignright NEW_CLASS'));

5) Theme Developer Options

Here’s a few options developers can use:
These one are to query the size of the images

// width of the thumbnails
get_option('thumbnail_size_w');
//  height of the thumbnails
get_option('thumbnail_size_h');
//  height of the medium resolution
get_option('medium_size_h');
//  width of the large resolution
get_option('large_size_w');
//  1 = Crop thumbnail to exact dimensions, 0 = Crop off
get_option('thumbnail_crop')

This example has been taken from WpEngineer.com

Here another example: If the size of a thumbnail is bigger than 100×100 and crop is activated, then the thumbnail should be resized to 100×100, otherwise use the original thumbnail.

if(get_option('thumbnail_size_w') > 100 && get_option('thumbnail_crop') == 1) {
the_post_thumbnail(array(100,100));
}else{
the_post_thumbnail('thumbnail');
}

For more info on the_post_thumbnail feature from wp 2.9 you can visit those links:

The always trusty Justin Tadlock:”Everything you need to know about WordPress 2.9’s post image feature
New in WordPress 2.9: Post Thumbnail Images
Using The New Post Thumbnail Feature In WordPress 2.9
The Ultimative Guide For the_post_thumbnail In WordPress 2.9

468 ad
  1. Nice article Brian! It really helped me out a great deal. I am having one problem. Maybe you know how to achieve this. I am using this code inside a theme:

    <a href="” title=”">

    It picks up the medium thumbnail, but it doesn’t link correctly with all the things I have tried including

    any ideas on how I can go about retrieving the “Large” picture URL to input in to the $thumbnail variable?

    Cheers

  2. Hi Matt, sorry I think the code you used didn’t get printed.
    you can use backticks or just omit the php tags. I need to find a way to ease the publish of code.

    Well to output your large “thumbnail” you can use this:

    How to return the image instead of displaying it

    In some scenarios, you might want to return the post image for use in your PHP code instead of displaying it.

    Here’s an example of that functionality:

    $image = get_the_post_thumbnail( $post->ID, ‘large’ );

    Note that the large in the code was specified by Matt M. from Automatic not to be used… so maybe an array there would be better. let me know if this helps.

    cheers.

Trackbacks/Pingbacks

  1. Post_thumbnail in WP 2.9 | ShareFavorite - [...] from:  Post_thumbnail in WP 2.9 Related [...]

Leave a Reply