Web Development Blog

by Ei Sabai Nyo

25 Jun, 2009

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>
Love what you've just read? Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
Your name:   Your email:  

7 Responses to “How to display a different header image for different pages in WordPress”

  1. V.C says:

    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. jo3 says:

    i love this, thanks

  3. Nick says:

    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

  4. eisabai says:

    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.

  5. jean paul says:

    Thanks man , i was looking for this !!!
    .-= jean paul´s last blog ..Palais des thés =-.

  6. Mandar Apte says:

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

  7. María del Mar says:

    Thank you so much!

Profile PicHello! Welcome to Web development blog! My name is Ei Sabai and on this blog, I write about web development, mobile app development, latest web technologies and the likes. Read more about me or have a look at some of the tips & resources I've written.
Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
We do NOT spam.
Your name:  
Your email:  

Tips & Resources

Tips & Resources
WordPress Web Hosting
Recommended web hosting providers for WordPress 3.0
iPhone Native App Development
Important steps into iPhone app development for beginners
iPhone Web App Development
Tips for iPhone web app development
Coupons for Web Developers
Get discounts on web hosting, domain names, templates, etc
10 Useful jQuery Snippets
Easy-to-use jQuery snippets for any website
HTML Email Newsletter
Step-by-step tutorial on how to code an HTML email newsletter
  • bluehost Hosting $6.95/month
  • Joomla Templates

Recommended Book

Categories