How to: Use multiple custom headers on a WordPress theme

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'); ?>
}
?>

Related Posts

Related Posts

No related posts.

20 Responses

Aug 28 2011 01:32

Actually you need to remove your “” after the semi-colon “;” for the above script to work. Other than that, it’s flawless. Thanks!

Aug 28 2011 01:35

Great, it stripped it from my comment. Ok, here’s trying to explain it in layman’s terms…

In order to make the above work, you have to remove the php open tag and the php close tag that are in front of each include statement and after each semi-colon respectively.

Sep 15 2011 07:51

Yes need to remove php starting and ending tag in include line like

include(TEMPLATEPATH.’/headercontact.php’);

Sep 30 2011 14:44

Looks really great but should be written as

if (is_page(‘contact’)){
(TEMPLATEPATH.’/headercontact.php’);
}
elseif (is_page(‘gallery’)){
(TEMPLATEPATH.’/headergallery.php’);
}
else {
(TEMPLATEPATH.’/headerdefault.php’);

With the appropriate PHP wrapper

Oct 26 2011 02:55

How would I do this if I want only the home page to have a different header? It is set to blog not static page btw. Thanks :)

Dec 07 2011 08:30

This is what I used to display a custom stylesheet and it worked great. You could probably substitute my wp_enqueue_style with (TEMPLATEPATH.’headername.php’);

Jan 05 2012 21:16

Unfortunately your code didn’t work for me, instead I used the following (which did the trick):

if (is_page(‘contact’)){
include(TEMPLATEPATH.’/headercontact.php’);}
elseif (is_page(‘gallery’)){
include(TEMPLATEPATH.’/headergallery.php’);}
else {
include(TEMPLATEPATH.’/headerdefault.php’);}

**Remember to add PHP wrap**

Feb 29 2012 01:42

Why not use the example from the wordpress codex:

Just customise as needed – header templates should be saved as header-name.php

Feb 29 2012 01:43

It didn’t like the cut and paste but the page is at http://codex.wordpress.org/Function_Reference/get_header

Mar 06 2012 12:15

I agree with David, you should use the get_header function instead of include:

if ( is_home() ) :
get_header(‘home’);

Apr 04 2012 10:36

Hello, thank you for all your comments,

But I try to use multy custom headers and I can’t do it !!!

I don’t know how exactly to whrite the code :

I want to custom my contact page, his name is : pagecontact.php
I have already create a custom header for this page : headercontact.php

and i don’t know where to put it ?

just on the page I want to custom ? the contact page ?
On the index page also ?
on the header page ?

If somewhone can help me it’s will be so great
thanks a lot
Delphine

Jun 12 2012 12:51

Hello Delphine,

Just change your headercontact.php to header-contact.php

and then change the get_header(); called at very top of pagecontact.php to
get_header(‘contact’);

Hope this helps.

Jul 20 2012 22:49

Totally agree with David Woodthorpe. We should use get_header(‘blog’) for blog category. It works well for me.

Aug 10 2012 17:29

Is there a way to extend this conditional header for each of the main sections of the site AND its child pages? So for “About” there is one header and any page under that About section has the same header?

Dec 04 2012 00:09

Anyone getting multiple headers I don’t know how to remove the original header

Jan 07 2013 07:23

Hi all, I’m new to WordPress but am considering using it for my website rather than pay for a host provider. I really would like to have a custom header for each page like you all are talking about, but I have NO idea where to put this php code… I only have a free wordpress page right now. Do I have to upgrade to the “customize” package so I can make these edits? Otherwise, when I create a new page, I can’t tell where to put the code…
Thanks so much for your time and help!

Jan 13 2013 07:49

Hi,

Here’s how I do it:

1. Install the Custom Post Template plugin.
2. Open the page.php or post.php file of your theme and save a copy of it as page-my-pic1.php.
3. Enter the following code on top of this file:

4. Copy your header file and rename it to say header-my-pic1.php

Make relevant changes in this header file (+ any other css files etc) to include your desired pic.

5. Now type the following code in the file (beneath the above code) that we earlier created i.e page-my-pic1.php

Upload all the files along with your pic1 etc.

6. Open the page where you want the desired pic on the header. Under Page Attributes you’ll have a Template drop down.

Select My Pic1 Template.

There you have it. Although this process is a bit long but I love it as it gives me the freedom to change pics on the click of a button. You can also use it to apply any other kind of changes to a particular post or page.

Jan 13 2013 08:00

Sorry the code didn’t appear above perhaps due to php tags:

Here it’s again:

code for point 3:

First it must be Inside php tags and it must be commented using php comments tag.:
/*
Template Name: My Pic1 Template
*/

Code for point 5: (It must also be inside php tags)

get_header(‘my-pic1′);

May 03 2013 23:13

Man, you really save my life!
Thanks so much!

May 14 2013 02:26

I did this and lost all CSS settings on the page…

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox