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.

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 …

Troubleshooting wordpress themes

here is a little trick if you need to check some variables, or try something new in your wordpress theme while the site is live.
use a conditional if statement to check that you are logged in as the original administrator, and only then do what you need to do. this way you can perform and display the neccesary tests, without the public seing anything.

if ('1' == $user_ID) :
echo 'you are logged in - this will not be shown to the public: <br/><br/>';
  // test routine begin //
echo 'cat-id , cat-slug , cat-name , number of posts <br/><br/>';
$categories= get_categories('');
foreach ($categories as $cat) {
echo $cat->cat_ID.' , '.$cat->category_nicename.' , '.$cat->cat_name.' , '.$cat->category_count.'<br/>';
  // test routine ends //
echo '<br/>end of logged in section <br/>';
endif; ?>

in this example, i displayed a full list of all categories with id, slug, and name.

the wordpress back-end (dashboard) is generally a good place to work, however for developers, some lists don’t show all the variables or parameters that would be useful for programming custom displays or functions. for instance, the IDs of posts, pages, categories, tags, etc. are not easily found in the back-end.

you can (and sometimes need to) build your own code snippets to show them on the spot where you are working with them.
this avoids a lot of guesswork.

and now you know how to hide these displays from public view.