Information Technology Reference
In-Depth Information
15
Implementing Post-Thumbnail Support in Your Theme
Before WordPress v2.9, you needed custom field hacks to place a decorative image on every post.
It's a lot easier now with the Post-Thumbnails feature, but you have to implement this feature in
your theme.
This is actually pretty simple. You need to open the functions.php file in your favorite text editor
(or the WordPress Theme Editor). Insert this code anywhere in the file:
/* Add support for thumbnail images attached to posts */
add_theme_support('post-thumbnails');
For backwards compatibility with older versions of WordPress, use this code to check for the exis-
tence of the add_theme_support function:
/* Don't break WordPress versions older than 2.9, but add support for thumbnail
images attached to posts */
if ( function_exists( 'add_theme_support' ) )add_theme_support( 'post-thumbnails' );
All the previous code adds a thumbnail to posts and static pages. If you prefer one or the other, try
these lines that make use of arrays:
add_theme_support( 'post-thumbnails', array( 'post' ) ); // Add it for posts
add_theme_support( 'post-thumbnails', array( 'page' ) ); // Add it for pages
To define the size of your thumbnails at 50 pixels square:
set_post_thumbnail_size( 50, 50 ); // 50 pixels wide by 50 pixels tall, box resize
mode
Want to include your thumbnail as part of the site's RSS feed? Add this code:
/* Include post-thumbnails in RSS feed. */
function insertThumbnailRSS($content) {
$content = '<p>' .the_post_thumbnail('medium'). '</p>' .$content;
return $content;
}
add_filter('the_excerpt_rss', 'insertThumbnailRSS');
add_filter('the_content_feed', 'insertThumbnailRSS');
Be aware that a post thumbnail requires two database queries, so each thumbnail slows down
your home page. This is yet another good reason to keep your home page to seven active posts.
That is, it will stop asking “what if?”
What happens if there is no post on the page? This can happen if a would-be visitor gets a faulty
link to one of your blog posts. The Loop offers something equivalent to a forlorn look, and tells the
visitor “Not Found. Sorry, but you are looking for something that isn't here.” It also includes a
Search WWH ::




Custom Search