0

I want to have one header page for the front page of the site and all other pages that follow by clicking "next" and a different header for all other pages, categories, articles, etc. on the site. Is there a way to do this within the header.php file or do I have to do something differently?

I'm using a free theme for the backbone of the site and have implemented a child theme to create a unique header as well as add functions separately so that I don't lose changes when the parent theme is updated, so I have minimal space to maneuver in order to affect these changes.

1
  • Ok. Now I realize why the disconnect. I need to clarify that I'm using a primary theme and am, right now, programming a child theme to handle additional functions and create the alternative header. Is there a way to insert the header swap into the index.php without creating a separate one since I'm using a child theme and have only, so far, duplicated the functions.php and header.php pages?
    – OscarGuy
    Commented Aug 22, 2017 at 21:29

2 Answers 2

0

Depending on the theme, in most cases you can simply edit header.php in your child theme and add a conditional:

if(is_home()) {
// code for the header displayed on home & paged
} else {
// code for the header on all other URLs
}

is_home() will return true if you are on what is referred to as the "blog posts index" - i.e. you're displaying a list of blog posts on the front page - as well as each additional page you reach through the "next" link.

8
  • Where would I put this? There's a lot of code in the header. Would I put it before the doctype tag or down in the body section?
    – OscarGuy
    Commented Jun 9, 2017 at 19:13
  • Wrap it around whatever changes. All pages should have a doctype, so you'd leave that alone, leave meta tags etc. that should be on every page alone, and go down to wherever the code needs to be conditional.
    – WebElaine
    Commented Jun 9, 2017 at 19:18
  • You can also create template_parts if you want to keep the two interchangeable headers completely separate, then call them conditionally in the header.php file. Makes for less code in a single file, which may or may not be useful in your case.
    – WebElaine
    Commented Jun 9, 2017 at 19:19
  • Ok. I tried to get this one to work and either I was putting the operations in the wrong spot or it just wasn't working. I still had the full header appearing on all pages.
    – OscarGuy
    Commented Jun 27, 2017 at 18:25
  • 1
    You might need to use if(is_front_page()) instead depending on whether you're showing a static Page or the default list of recent posts on the homepage.
    – WebElaine
    Commented Jun 27, 2017 at 21:44
0

It's possible to have multiple header files in a theme, and to have your templates load them based on conditions you choose.

You already have a header.php file that gets loaded when a template calls get_header(); you can create a new one called header-front.php with the markup for your home page and any other pages you choose, and load it instead by calling get_header( 'front' ). The parameter that gets passed is the slug of the file (everything in between "header-" and ".php" in the file name.

3
  • Would I put the "front" markup in the index.php file only then?
    – OscarGuy
    Commented Jun 27, 2017 at 18:25
  • You would have all the appropriate header content in header.php and header-front.php, and call them with get_header() and get_header( 'front' ) in index.php. They should be wrapped in conditional checks for is_home() || is_front_page() to call the right header for what you need. If true, call header front, else call header. Commented Jun 29, 2017 at 3:03
  • My apologies for the way delayed response. I get busy on other things and forget I'm working on this as a side project. I added a note to my original post above explaining that I'm doing this as a child theme, which may complicate how I can handle it.
    – OscarGuy
    Commented Aug 25, 2017 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.