This is quite simple to implement, using core post-thumbnails
functionality.
First, you need to add Theme support for the feature, via functions.php
:
<?php
function wpse54920_setup_theme() {
// Support post thumbnails
add_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'wpse54920_setup_theme' );
?>
Next, you need to define your custom image sizes, using add_image_size(). Let’s say you want the following custom image sizes:
archive-index-featured
, 150px x 100px, hard-croppedpost-header-featured
, 250px wide, box-resized
Define them as follows (building on our code above):
<?php
function wpse54920_setup_theme() {
// Support post thumbnails
add_theme_support( 'post-thumbnails' );
// Add Archive Index Featured image size
add_image_size( 'archive-index-featured', 150, 100, true );
// Add Post Header Featured image size
add_image_size( 'post-header-featured', 250, 9999, false );
}
add_action( 'after_setup_theme', 'wpse54920_setup_theme' );
?>
Now, you simply need to output the appropriate image size in the appropriate template file, or via the appropriate conditional. For simplicity, I’ll use the latter approach:
<?php
if ( has_post_thumbnail() ) {
// Determine image size based on context
$featured_image_size = ( is_single || is_page() ? 'post-header-featured' : 'archive-index-featured' );
// Output the post featured image
the_post_thumbnail( $featured_image_size );
}
?>
Alternately, you could simply call the_post_thumbnail( 'post-header-featured' )
in single.php
and/or page.php
, and call the_post_thumbnail( 'archive-index-featured' )
in other template files.