Snippets, WordPress, xHTML

How to display a different header image for different pages in WordPress

In header.php file of my WordPress theme, I have the following code to display a different header image for different pages.

<?php
  if (is_page('about')) {
      $image = 'image_about.jpg';
  } else if (is_page('projects')) {
      $image = 'image_projects.jpg';
  } else if (is_page('services')) {
      $image = 'image_services.jpg';
  } else if (is_page('contact')) {
      $image = 'image_contact.jpg';
  } else {
      $image = 'image_default.jpg';
  }
?>
<div id="header">
<img src="<?php echo bloginfo('template_url'); ?>
/images/<?php echo $image;?>" 
alt="<?php bloginfo('name'); ?>" />
</div>

7 thoughts on “How to display a different header image for different pages in WordPress

  1. Thanks a lot !
    I think this tip is useful for the magazine blog.
    My site is also an magazine blog so I just need this tip.

  2. Hi, can I show also a swf file?? I think yes, but I want to know your opinion before the creation of the flash file.

    Thanks

  3. Nick: Yes, you can. If you have different flash files for different pages of the website, you can use the same code but replace the name of the image with the name of the flash file (eg: instead of image_about.jpg, it will be about.swf). And then you will need to change your flash embed code (eg: swfobject) to use the variable that you specified.

  4. This code is not working for my “blog” page…!!!! Why??? for other pages code is working as described.

  5. Thank you so much!

Comments are closed.

Twitter
LinkedIn
YouTube
Instagram