Do you ever wanted to have multiple headers on your WordPress blog? If yes, here’s a clear and concise tutorial that will help you to get themost out of your blog headers!
Do you ever wanted to have multiple headers on your WordPress blog? If yes, here’s a clear and concise tutorial that will help you to get themost out of your blog headers!
The first thing to do is to create header files. Create as many different headers as you want. In this exemple, I have created 3 custom headers from my theme default file header.php, names headercontact.php, headergallery.php and headerdefault.php.
By using the php include() function and WordPress conditional tags, we can define custom headers easily.
Replace the content of your header.php file with the following code:
<?php
if (is_page('contact')){
<?php include(TEMPLATEPATH.'/headercontact.php'); ?>
}
elseif (is_page('gallery')){
<?php include(TEMPLATEPATH.'/headergallery.php'); ?>
}
else {
<?php include(TEMPLATEPATH.'/headerdefault.php'); ?>
}
?>
24 Responses
I am now a fan. This site is going to do very well if it continues to come up with “great recipes”.
@Neowster: Thanks a lot for your kind words, I really appreciate it!
This is cool.
Please tell me also how to make different headers appear randomly or in rotation.
Thanks & ciao
alexander
Oooh, that is so cool. I think I will implement it into one of my wordpress designs!
I’m still a PHP noob, but this was giving me “unexpected <” errors until I stripped out the inline tags. Once I did, things worked perfectly:
What would be the conditional tag if you want a different header for index.php?
Thank you
Hello,
Great tutorial!
I have a question. How can get this applied automatically when I create a new Page in Wodpress? I have five different headers and five main menu alternatives. The user will be creating new pages under some of the main pages and the correct header needs to be applied. For example if they create a new page under “Gallery” the gallery header needs to show but it will ofcourse have a different name. So every new page created would have to be manually added in the php code so it will show the right header.
thanks
Exactly what I was looking for ! Thx
i may have found a much simpler method.
in the header.php, do the following:
<body id="”>
then in the css, apply the background image based on the page title, such as:
body#About {
background-image: url(../images/headerAbout.gif);
}
arg! the post eliminated my php code!
<body id="<body id="">I’ll try this one last time…
[code]
<body id="">
[/code]
Much easier solution than what I’ve tried in the past. Keep up the good work.
I was wondering if you know a method for doing the same thing when the wordpress home template (home.php)?
Paul use the WP template tag and create specific page templates (copy your page template file and duplicate it then insert the following code with the correct name. It will display as an option when saveing editing pages
I found the same problem as Dave above, that I was getting unexpected “<" errors. Once I stripped out the inside PHP tags, leaving only the opening and closing PHP tag, then it worked great. Here's the (barely) modified code that worked or me:
[code]
[/code]
Your code doesn’t work….it should be…
Plain and simple. Thanks a lot.
After a lot of experimenting before I read the comments, I finally got this working by removing the embedded <?php tags. Before that I was getting completely blank pages. The php documentation states the embedded tags are needed with include, but maybe my version of WordPress was smart enough to figure it out.
Anyway, thanks. This is just what I needed
Sorry but I’m lost, not working for me. A little help, please.
In this example:
if (is_page(‘contact’)){
Is the “contact” the actual name of the file, as in: contact.php? Or are the pages being named in some other way?
Also, is there a way to list the index page? if (is_page(index’)) didn’t work for me. Thanks.
Trackbacks: