Recently I was working on a website where my customer was starting a WordPress blog and needed a different header on each page. Not post, but page. In this particular situation, my client is using WordPress as a content management system for static content pages only, and not for a blog. There is some very simple PHP code that I used in my header.php file, and this code can be helpful for you especially if you are not an expert in PHP, but just need a solution.
Locate your header.php file and located the <div> for your header. Replace it with this code:
<?php if (is_front_page()) { ?>
<div id="header"> <!– This is the Header id that you want on your Front Page –>
<?php } else { ?>
<div id="<?php echo $post->post_name; ?>"> <!– An alternative header is defined, based on the page title –>
<?php } ?>
Now you define each “header” div in your style.css file. The name of the <div> is associated with each static page title. So for example, if you have an About page your div would be #about in your CSS file. It’s a pretty simple approach to getting a different header on each page for WordPress. This code would of course work for any div or class. Most times people use it for the Body class. In my case I only needed a different header and that worked fine for me.
I think the code here is so simple, it’s almost a waste of time to create any kind of plugin to do the same thing. Might as well as just use the simple code to make it happen. If anyone has a different/better way of accomplishing this same task please post your thoughts. Good luck with your WordPress site and I hope this helps you in Creating a Different Header on Each Page for WordPress!

Uffe
August 19, 2008
Hi ! Great thanks for this one, but i can’t get this to work, i replaced my with the code, in my header.php but the script doesn’t seem to recognize #headerbg in my stylesheet, nor the page title. Can’t see what i am doing wrong, i am confident i have not misspelled, please help – i really need this one!
Thanks!
nycbone
August 20, 2008
That WAS simple and especially after messing with another solution for hours.
Working on my first web development job using WP and I needed a quick solution till I learn the ropes. I’m using this for the exact same reason you are.
Just wanted to say thanks!
Wordpress newbie – nycbone
stress
October 14, 2008
Thanks man – didn’t try it yet, but that was exactly what i was looking for!
Dee Yan
January 6, 2009
i had put that scripts inside my header, but it didn’t work. Thanks for what you do.
here what i write :
<div id=â€Âpost_name; ?>â€Â>
Jessica
January 8, 2009
Hey, thanks so much for this. You’ve saved me a lot of time. I have a quick question, though. What happens if my page title has two words; ie. About Us, or Contact Us… How do I reference that in the CSS? Do I put a hyphen/space/underscore between the words?
Like, #about-us or #about_us
Or do I need to add something to the php to convert spaces to hyphens?
Thanks so much!
Jessica
January 8, 2009
…actually disregard above question. The hyphen worked. I just didn’t have the correct permalink structure on that page.
Thanks again!
Emily
January 28, 2009
Hi, thanks so much for sharing this — it’s exactly what I want to do. I use the code and have been successful with adding one extra header, but am having trouble adding more that that.
All my div tags are in place in my stylesheet, but adding more divs to your php code above is where I hit my snag.
I have tried duplicating the entire script within the header.php for each ‘page’. I have also tried duplicating the div id only within the header.php. When I do either of these, all pages adopt the most recent header that I’ve added (except for the front page).
I know CSS, but not PHP. I suppose my question is, how do you sequence the code above if you have, let’s say, 5 different headers to use on 5 different pages.
Thanks in advance!
Mark
February 16, 2009
Hi
I guess the above is just for pages.
How do you set it up so that you have one header for index.php and a different header for the other files such as single.php, 404.php, etc?
Thanks
Daniele
March 29, 2009
Great script only few corrections: remember to close your div and to replace with ” your â€Â
Mahmoud Taji
April 1, 2009
Hi! This is the code I need… I’m using the emptiness theme by qoqoa. and I want to use wordpress as a CMS for my whole site… I dont know how to program php or css… I am the worst kind of noob… and to top all of that off qoqoa used a div= main splash instead of header image or whatever…. I know that your code is the exact thing I want… a code that requires very little fiddling in the php and a few additions to the css… but I dont know what to put int he css… you only put what the php is supposed to look like..
can you help?
taji
Cintia
April 12, 2009
Thanks. This has solved my problem. the code on the Codex page just doesn’t work for me. codex.wordpress.org/Function_Reference/is_home
You should share this example on Codex!!! Thanks again and have a nice day!
Jason
April 30, 2009
HI,
My header images are working great thanks to your code, but it is not working on the POST page. My home page is static.html and my post page is not my home page. I cannot get a header to show on the post page. Has anyone ran into this issue? Any help is greatly appreciated. thanks
Gloria
June 8, 2009
How do I use this code if I have changed the frontpage settings under reading? My blog is no longer the frontpage yet its not connected to the page.php, its connected to index.php. For this reason I am not able to give my blog page an image with your code…it does not recognize the blog page. Is there anyway around this? How would I adjust the this
to call the blog page instead of the frontpage?
Thank you for your help!
Kristina
July 7, 2009
Jason-
This is what I figured out to have a header show also on the posts page:
/* the name of your id */
<div id="post_name; ?>”>
Then add this right underneath:
/* the name of your id */
Kristina
July 7, 2009
Opps, I think it deleted my code, here it is again for those who need the header to show up in post pages as well: (replace & with < ) and "wrap out" is the name of my div id.
&?php if (is_front_page()) { ?&
&div id="wrap-out"&
&?php } else { ?&
&div id="post_name; ?>”&
&?php } &
&?php if (is_single()) { ?&
&div id=”wrap-out”&
&?php } ?&
Neelesh
November 8, 2009
Fantastic. ditto nycbone. Thanks – was trying to create a different header.php file and call it by get_header_home.. kept failing. much simpler and neater solution. Thanks again.
Tor-Arne
November 10, 2009
Thx for a great tip! It works perfect, it´s only one thing I am wondering about.
When I have sub-pages to a page. And I go into that sub-page, thea header disappears!
Do you know why it´s like that? I had a friend who took a look at the code, and said nothing was wrong, but something has to be.
Thx for any help!
peter
January 31, 2010
I tried to insert the code as instructed, but I get a syntax error and the site does not render. I am also trying to create different headers for about 12 pages. Here’s what I tried (uppercase indicates my changes). Does the div need to be closed before the php code is closed? Thanks for any help you can offer.
<div id=”PAGE_TWO; ?>”>
darren
February 1, 2010
Thanks..
My first WP site and this code worked spot on …. first time.. just what I needed and Im not great with code .. but now i have a flash video header on the home page and jpg and png header on the rest of the site,.. Thanks to your code!!
LilMina
February 8, 2010
So, if i want to use one header in frontpage and different header to all oyher pages, how to use it?
Ashley
April 4, 2010
Thanks! This was really helpful, however I had to change the “” marks because they were different and didn’t work in my editor.
Kezza
May 17, 2010
If you want to add different banners per page and have a static banner for the posts use the below code.
<div id="post_name; ?>”>
Kezza
May 17, 2010
<div id="post_name; ?>”>
Kezza
May 17, 2010
Grrr Replace * with
*?php if (is_front_page()) { ?*
*div id=”banner”*
*?php } else { ?*
*div id=”*?php echo $post-*post_name; ?*”*
*?php } ?*
*?php if (is_single()) { ?*
*div id=”banner”*
*?php } ?*
Sam
May 21, 2010
Hi this is great, and has been easy to implement.
I’m using it to create slideshows with different content in the header for each page, and it works well.
However, I don’t want it to do this if I’m looking at a single post or search result. I want these pages to display the home page images. Any ideas?
I’m using
…
/pic_01.jpg” alt=”Picture” width=”920″ height=”530″/>
…
…
<img src="_post_name; ?>/pic_01.jpg” alt=”Picture” width=”920″ height=”530″/>
…
Sam
May 21, 2010
Mmm code got mangled…
Should be like this (replace square brackets with tag arrows)
[?php if (is_front_page()) { ?]
[!-- home page --]
…
[img src="_home/pic_01.jpg" alt="Picture" width="920" height="530"/]
[?php } else { ?]
…
[!-- other pages --]
…
[img src="_[?php echo $post-]post_name; ?]/pic_01.jpg” alt=”Picture” width=”920″ height=”530″/]
…
[?php } ?]
Sam
May 21, 2010
It was easy. Here for anyone else with a similar problem.
[?php if (is_page()) { ?]
[!-- page --]
…
[?php } else { ?]
[!-- everything else --]
…
[?php } ?]
Blanca Kett
June 11, 2010
Its just sometimes people seem to get themselves tied up in unnecessary knots over something that?s very simple.
John
June 23, 2010
This may have already been addressed, but I am a bit confused looking back through the comments. Here’s my situation…
1) Different headers for each PAGE works great! Thank you.
2) However, I cannot get the header for the blog, i.e. posts, to work.
Recap: Headers for Pages works. Header for Blog Posts does not.
Suggestions?
Thanks very much!
lucy
July 8, 2010
Hi
I’m very new to css and php, I’m a quick learner though and got most of it right but could someone tell me –
How do I ‘define each “header” div in the style.css file’
Sorry if it’s really basic! I’m trying to get different headers on each page and I can do it using the above code so my homepage image is different to every other image but every other image is the same and I need idfferent ones on each page – hope this makes sense!
Jackie
July 18, 2010
How do I do the step below? Can you give an example of the code showing how to do this?
Now you define each “header” div in your style.css file. The name of the is associated with each static page title. So for example, if you have an About page your div would be #about in your CSS file. It’s a pretty simple approach to getting a different header on each page for WordPress. This code would of course work for any div or class. Most times people use it for the Body class. In my case I only needed a different header and that worked fine for me.
Crystal
July 23, 2010
Thank you so much!! This has made things so much easier for me. I was getting prepared to set up multiple templates for each page. Your way was so much easier.
Carly
August 10, 2010
Thank you for this information! It worked perfectly! Was exactly what I needed. Thank you so much for sharing.
Dia
January 28, 2011
Hello!
Sorry, newbie as well, Im using the Graph Paper Press theme F8 Lite and I cant exactly find the header in my header.php file… I post the code, if somebody could help me out which part of the code I should replace would be very appreciated
<html xmlns="http://www.w3.org/1999/xhtml" >
<meta http-equiv="Content-Type" content="; charset=” />
» Archive
<meta name="generator" content="WordPress ” />
<link rel="stylesheet" type="text/css" href="” />
<link rel="stylesheet" href="/css/print.css” type=”text/css” media=”print” />
<link rel="alternate" type="application/rss+xml" title=" RSS Feed” href=”" />
<link rel="pingback" href="” />
<body >
<a href="” class=”feed”>posts <a href="” class=”feed”>comments
<a href="/” title=”Home”> <a href="mailto:”>
ID ) &&
( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( ’950′, ’425′ ) ) ) &&
$image[1] >= HEADER_IMAGE_WIDTH ) :
// Houston, we have a new header image!
echo get_the_post_thumbnail( $post->ID, ’950×425′ );
else : ?>
<img src="” width=”" height=”" alt=”" class=”headerimg” />
<?php
if ( is_home() && $paged
alex
April 8, 2011
i need help
i used the following code
<img src="/images/home_banner.jpg” alt=”frontpage image” width=”965″ height=”126″ border=”0″>
<img src="/images/investcrit_banner2.jpg” alt=”image2″ width=”965″ height=”126″ border=”0″>
—————
how to write if and else more conditions like
if ( is_page( ’42′ ) )
i want to show the page related images
how to write another else condition that should be related to the page.
and final condition should be else if to all pages.
please help………….
Klara
April 12, 2011
Thank you so much!!!
I’ve been trying to do this for days and I’ve tried several ways that didn’t work.
Your guide made me do it in 5 mins! I’m SO grateful!
Luis
April 26, 2011
Hey Thanks for the comments and the code. It’s very helpful.
I’m a beginner so i did struggle a little to implement but it worked.
Just wanted to add the following:
in this case i set a rule for an spefic page and everybody else to have the same.
Luis
April 26, 2011
ahh. it deleted the code.
Cybernautix Peter
May 9, 2011
Thanks for the info. The
[?php if (is_page()) { ?]
[!-- page --]
…
[?php } else { ?]
[!-- everything else --]
…
[?php } ?]
worked for me when wanting to change the header image dependng on whether or not the visitor is on a page or a post.