Featured Image in Header of Archive Pages in Twenty Eleven

To show the ‘featured image’ of the top post as header image in an archive page in the Twenty Eleven theme, edit header.php in the child theme;

change this section:

<?php
/*
* The header image.
* Check if this is a post or page, if it has a thumbnail, and if it's a big one
*/
if ( is_singular() && has_post_thumbnail( $post->ID ) &&
( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
$image[1] >= $header_image_width ) :
// Houston, we have a new header image!
echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
else :
// Compatibility with versions of WordPress prior to 3.4.
if ( function_exists( 'get_custom_header' ) ) {
$header_image_width  = get_custom_header()->width;
$header_image_height = get_custom_header()->height;
} else {
$header_image_width  = HEADER_IMAGE_WIDTH;
$header_image_height = HEADER_IMAGE_HEIGHT;
}
?>
<img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
<?php endif; // end check for featured image or standard header ?>

to:

<?php
/*
* The header image.
* Check if this is an archive and if the latest post has a thumbnail, and if it's a big one
* or Check if this is a post or page, if it has a thumbnail, and if it's a big one
*/
if ( is_archive() ) :
the_post();
$archive_thumb = '';
if( has_post_thumbnail( $post->ID ) &&
( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
$image[1] >= $header_image_width ) :
$archive_thumb = get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
endif;
rewind_posts();
endif;
if ( is_archive() && $archive_thumb ) :
echo $archive_thumb; //the featured image of the latest post in the archive
elseif ( is_singular() && has_post_thumbnail( $post->ID ) &&
( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
$image[1] >= $header_image_width ) :
// Houston, we have a new header image!
echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
else :
// Compatibility with versions of WordPress prior to 3.4.
if ( function_exists( 'get_custom_header' ) ) {
$header_image_width  = get_custom_header()->width;
$header_image_height = get_custom_header()->height;
} else {
$header_image_width  = HEADER_IMAGE_WIDTH;
$header_image_height = HEADER_IMAGE_HEIGHT;
}
?>
<img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
<?php endif; // end check for featured image or standard header ?>

Different header image for each page in wordpress

how to show a different image for each page, for instance in the header?

simple, all you have to do with this method is to create an image file with the page ID as part of the image name.

the code for this is really short:

<?php $page_id=get_the_ID();
if(is_page()) { $image='head-image-'.$page_id.'.jpg'; };
if(!file_exists(TEMPLATEPATH.'/images/'.$image)) { $image='head-image.jpg'; }
echo '<img src="'.get_bloginfo('template_url').'/images/'.$image.'" alt="" />'; ?>

this is all.

edit:
if you are using a child theme, and are storing the images in the child theme’s /images folder, replace any ‘template’ references with ‘stylesheet’ – for example:

<?php if(is_page()) { 
  $page_id=get_the_ID(); $image='head-image-'.$page_id.'.jpg'; 
  if(!file_exists(STYLESHEETPATH.'/images/'.$image)) { $image='head-image.jpg'; }
  echo '<img src="'.get_stylesheet_directory_uri().'/images/'.$image.'" alt="" />'; 
}; ?>

end of edit

first step: create an image file name including the page ID (on pages only);
second step: check if a matching image file exists in the images folder of the theme;
third step: if yes, echo the image name; otherwise echo a default image name.

edited and extended:
if the header image is a css background image, this code can be easily adapted:
put it into an embedded style declaration into the <head> section of header.php:

<?php $page_id=$post->ID;
if(is_page()) { $image='head-image-'.$page_id.'.jpg'; 
if(!file_exists(TEMPLATEPATH.'/images/'.$image)) { $image='head-image.jpg'; }
echo '#header { background: url(' . get_bloginfo('stylesheet_directory') . '/images/'.$image.') center top no-repeat; }'; 
}; ?>

this can even be changed to be used to show different header images for each post …