Custom Header.php on different pages

Can you have multiple custom header.php files? Say I want one header on the homepage and another on the inner pages?

Sorta, I think.

At least for the case you are talking about, you could use the is_current_url() function in header.php to check if you are on the homepage, and branch behavior from that.

<?php
if(is_current_url('/')) { echo "<h1>FRONT</h1>"; } 

?>

I suppose that branch could involve including different files, which would be pretty close to having different header.php files.

More complex checks would involve a lot more, and would be much more error-prone, especially when it comes to plugins.

The header.php file can display a different header based upon a variable passed to it from any calling script.

Imagine a protocol between header.php and the main page index.php, where a variable, say $context, is passed from index.php to header.php. The protocol is defined in this way: if $context has a value of 'main', the header.php will have a special "main" display
and when it has any other value, or is not available or not set, it will have a standard display.

This is implemented by passing the "context" variable to header.php in the echo head() line within index.php.

<?php
echo head(array('bodyid'=>'home', 'context' => 'main'));
?>

Other script pages can call header with $context set to some other control value, such as 'inner'.

<?php
echo head(array('title'=>$pageTitle, 'context' => 'inner'));
?>

In header.php, add code to handle the different display options by checking the value of $context. Write the code so it handles cases
where the caller does not pass the value in, so it works with all existing scripts, which do not pass in a value for $context.

<?php
// establish which banner to display based on the calling context.
// main index.php will call this with $context set to 'main' to tell us to do a "main" display.
$fDisplayMain = false;
if ( ! isset($context) ) {
    $context = 'inner';
}
if (strcmp('main',$context) == 0) {
    $fDisplayMain = true;
}
?>

Later in header.php, have conditional code that displays differently based on the context.

<?php
// display variant banner depending on context, discovered above.
if ($fDisplayMain) {
    echo '<h1>Main Page</h1>';
} else {
    echo theme_header_image();
}
?>

So this is my header.php

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="HandheldFriendly" content="true">
<?php
if (isset($title)) {
	$titleParts[] = strip_formatting($title);
}
$titleParts[] = option('site_title');
?>
<title><?php echo implode(' | ', $titleParts); ?></title>
<?php queue_css_file('style'); echo head_css(); ?>
<?php queue_js_file('jquery-1.8.1.min'); ?>
<?php queue_js_file('functions'); ?>
<?php echo head_js(); ?>

<link rel="shortcut icon"  href="<?php echo get_theme_option('Base URL'); ?>/themes/images/favicon.png" type="image/x-icon" />

</head>
<body>
<!--outer-wrapper-->
<div id="outer-wrapper">
  <!--blue-sidebar-->
  <div id="blue-siderbar" class="visible-desktop">
    <div id="sidebar-logo"><a href="<?php echo get_theme_option('Base URL'); ?>"><img src="<?php echo get_theme_option('Base URL'); ?>/files/theme_uploads/<?php echo get_theme_option('Logo 1'); ?>" alt=""></a> <img src="<?php echo get_theme_option('Base URL'); ?>/files/theme_uploads/<?php echo get_theme_option('Logo 2'); ?>" alt=""><img src="<?php echo get_theme_option('Base URL'); ?>/files/theme_uploads/<?php echo get_theme_option('Logo 3'); ?>" alt=""> </div>
  </div>
<!-- wrapper -->
  <div id="wrapper">
    <!--header-->
    <div id="header">
      <div id="inner-header-phone" class="visible-phone">
        <div class="inner"><a href="#" class="logo-phone"></a>
          <button class="btn-menu-phone" onclick="revealMenu()">Menu</button>
        </div>
      </div>

      <a href="<?php echo get_theme_option('Base URL'); ?>/advanced-search/" class="advance-search visible-desktop">Advanced Search</a>
      <div class="menu">
       <?php echo public_nav_main(); ?>
      </div>
      <span class="search-phone">
      <?php echo search_form(); ?>
      </span> </div>
    <!--/header--> 

    <!--map-->
    <div id="map"> <iframe width="666" height="337" src="//www.youtube.com/embed/AKytUjK6Ihg?rel=0" frameborder="0" allowfullscreen></iframe><!--<iframe width="666" height="337" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="<?php echo get_theme_option('Map URL'); ?>"></iframe>--></div>
    <!--/map--> 

    <!--content-->
    <div id="content">

I just want to show the Map div on the homepage, but on no other pages. So, Bob would you solution work for this?

Drew, the simple answer is yes. But...

My answer was a general answer aimed at someone who can follow PHP code and would like to have a different look on the main page (the banner for instance), while having another banner on every other page, or for someone wanting to have any sort of special processing in the header based on where it is used.

This usage is not clearly explained anywhere when I went looking for it, so my answer was a quick overview to give the next person having such a question a hint on how things work and how headers and footers coordinate with other scripts. Your post has a great subject line so that anyone searching this question will find this thread.

In your case, Patrick's clever answer is a better choice.

It uses a single if-statement that is only true on the main page and only the header.php file is changed.

Using it still requires a thoughtful eye toward how PHP and HTML works, but it's at the introductory level you would want to learn if your ambitions for the web site include mashups.

Here's Patrick's answer, with comments added to the PHP code, and reformatted so that the HTML that will only be generated on the main page is native HTML instead of HTML generated by a PHP echo statement. The example code causes a welcome header to be shown only on the main page.

<?php
// If the URL to this page is '/', this is the main page.
// Every other URL will be something like '/items/show/23'
//       or '/collections/browse'
if (is_current_url('/')) {
?>
    <h1>Welcome to the Website's Main Page</h1>
<?php
 }
?>

If the intent at your web site were to have the link to the Advanced Search on the main page only and have it gone on every other page, your header.php would be changed in this way in the section that emits the Advanced Search link.

<div id="inner-header-phone" class="visible-phone">
  <div class="inner"><a href="#" class="logo-phone"></a>
    <button class="btn-menu-phone" onclick="revealMenu()">Menu</button>
  </div>
</div>

<?php
// When header is on main page (URL of '/'),
//    show the Advanced Search link.
if (is_current_url('/')) {
?>
    <a href="<?php echo get_theme_option('Base URL'); ?>/advanced-search/"
       class="advance-search visible-desktop">Advanced Search</a>
<?php
}
?>

<div class="menu">
 <?php echo public_nav_main(); ?>
</div>
<span class="search-phone">
<?php echo search_form(); ?>
</span> </div>

How would you change your header.php, following that example pattern, so that the map div only shows up on the main page?

I enjoyed the placeholder youtube video embedded in the map div of your header.php. Thanks.

History of Tinner Hill, Falls Church VA
youtube.com/watch?v=AKytUjK6Ihg

Bob,

Many thanks for expanding the usefulness of this thread, and my response. Much appreciated!

Patrick

Ah, so is_current_url is a lot like is_home in WordPress, which is the codex I mess around with most. This makes sense now! Thanks Bob & Patrick.

Could this work as well for creating a custom navigation on certain pages?

I _think_ so, but would involve some work to replace what public_nav_main would normally output.

An alternate approach to customizing navigation on particular pages might be to use the public_nav_main filter, changing the nav array based on the particular page.

Thank you!

I am frankly no php coder but I have searched high and wide for what I thought was a simple request and can't find anything.

What I am looking for is to have the header image be based on the page ID. If page#xxx then image YYY. Is that possible? I would think it would have a lot of applications if it's doable.

Thank you.