Create multiple headers in WordPress

The Problem

I’ve enounterd the need for this on several projects and thought it might help. In this case, I didn’t want the featured slider to appear on every page, as it was located in the header. You too, may want to display page-specific headers based on certain conditions. Well, it’s not that hard with the “if” and “else” statements. Get ready to learn some very basic PHP!

The Fix

First, open your header.php file and copy and paste its contents into a new file. Name this new file “headerwithnofeaturedslider.php” or whatever is more semantic to you. Make sure you remove the offending featured slider portion (or include/remove what you want from this version of the header). Save and upload this new header file to your site.

Now you have two headers; you could easily make another, and another, and so on. Next we need to call them to appear on the pages in WordPress. In my case, I wanted to include the “headerslider.php” only on my home page and a unique header on my blog Every other page would load the normal header. So in my index.php file I replace the normal header tag with to include the new header file we created, like so:


}
elseif (is_page('blog')){
	
}
else {
	
}
?>

What we have here is a code snippet that will load any of three headers depending on the page template used.

Let’s say that you wanted to load a certain header based on categories or tags even. You could do something like this:




 ?>




All other pages will load the regular header. As you can see, it’s very simple. The most involving part would be changing your headers.

No related posts.

10 Responses to “Create multiple headers in WordPress”

  1. Rob November 20, 2008 at 6:54 am #

    Hi Mike, I’m new to developing on wordpress and I’m currently developing a site that I need to be able to change the header file on every page(its not just reloading an image, im using a plugin to load in additional content) .
    (I’m not experienced in php) How can I get the different pages to call the different headers?
    ?

  2. admin November 20, 2008 at 7:48 pm #

    Hi Rob,

    Can you duplicate the header files, and name give them names for each page, and then use the include path shown above to call each one on a particular page?

    If you link me to the problem I’ll take a look in case it’s more complicated than that.

    thanks

  3. Joe December 28, 2008 at 12:39 pm #

    Rob this worked great and is exactly what I was looking for. You’re a gentleman and a scholar!

    Thanks!

  4. Joe December 28, 2008 at 12:40 pm #

    Whoops, didn’t mean to call you the wrong name there Mike! Sorry!

  5. Raymond Selda May 9, 2009 at 1:22 pm #

    Very nice tip here Mike. Thank you for sharing this one. This will definitely come in handy.

  6. mark matsusaki January 3, 2011 at 11:11 pm #

    Thanks for posting this – but I’m using WP 3.x and the file names / structure seems to have changed. I’m unable to find header.php

    Am I missing something ?

    • Mike January 3, 2011 at 11:15 pm #

      Hi Mark

      You’re most welcome, and I’ve sorta fallen off the WP development platform, but I did just log into my FTP account. I do see a “header.php” file in all themes, even the ones on WP 3+. You should def. see that file. What theme are you using?

  7. Lise July 27, 2011 at 3:45 am #

    Hi Mike, I am using this code and getting the following error:

    ‘Parse error: syntax error, unexpected ‘<' in /usr/www/users/ypowimutud/test11/wp-content/themes/coraline_child/index.php on line 9'

    Code Snippet:

    <?php
    /**
    * @package WordPress
    * @subpackage Coraline
    * @since Coraline 1.0
    */
    if (is_page('home')){

    }
    elseif (is_page(‘blog’)){

    }
    else {

    }
    ?>

  8. Mike October 12, 2009 at 12:27 pm #

    Yes, that’s correct. You will use PHP if/else statements to load certain headers depending on the circumstances.

  9. Mike November 1, 2009 at 11:27 pm #

    Hi Kristy – Yea, so my syntax highlighter was messed up, but I just went through and fixed up the code. Take another look and it should make sense. I included a second example of how to load headers based on categories, just to further demonstrate the idea.