WordPress.org

Support

Support » Themes and Templates » Load different header on Homepage

Load different header on Homepage

  • [Note: I’m not too proficient with php so this might be a very noobish mistake]

    I need to load a different header section on my homepage from the rest of the site. From what I have researched it seems I need to implement a piece of code in my header.php:

    something like:

    if ( is_front_page() ) :
      get_header('header-home.php');
    elseif ( is_404() ) :
      get_header('404');
    else :
      get_header('header-site.php');
    endif;
    ?>

    I’ve created two copies of my header.php namely header-home.php and header-site.php. One with and the other without the jquery. The code was placed right below <div id=”wrapper” class=”hfeed”> but above the <nav> section. When I fire up my site no header is loaded. It seems like the php isn’t even read!

    Any ideas what could be wrong?

    Thanks for your time! 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator Andrew Nevins

    @anevins

    Forum moderator

    Can you var_dump() or exit() within if statements, to see whether you’re getting inside any one of them?
    http://php.net/manual/en/function.var-dump.php
    http://php.net/manual/en/function.exit.php

    Thanks for the reply Andrew…

    I tried this [hope it’s right]:

    <?php
    if ( is_front_page() ) :
      get_header('header-home.php');
    else :
      exit("unable to open file ($filename)");
    endif;
    ?>

    Homepage opens up as usual with no header and as soon as I go to the any other page it outputs unable to open file ($filename). So I guess the php is running fine but something is wrong with my header :(.

    I have a javascript running: Part of my header.php code looks like this…

    </head>
    
    <body <?php body_class(); ?>>
    <div id="wrapper" class="hfeed">
    
    <?php
    if ( is_front_page() ) :
      get_header('header-home.php');
    elseif ( is_404() ) :
      get_header('404');
    else :
      get_header('header-site.php');
    endif;
    ?>
    
    <nav class id="clearfix">

    And my header-home.php reads as:

    <?php wp_head(); ?>
    
    	<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery-1.6.3.min.js"></script>
    	<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery.spritely-0.6.js"></script>
        <script type="text/javascript">
    
            (function($) {
                $(document).ready(function() {
                $('#hill1').pan({fps: 30, speed: 0.5, dir: 'left', depth: 70});
                });
            })(jQuery);
    
        </script>
    
    </head>
    
    <body <?php body_class(); ?>>
    <div id="wrapper" class="hfeed">
    
    <header >
    
    <div id="header">
    <div id="banner" class="banner">
    
    		<div id="hill1" class="banner">
    			<div id="siener">
    				<div id="logo_banner">
    					<!-- <div id="logo"></div> -->
    					<div id="image">
    					<a><img class="header" src="<?php bloginfo('template_directory'); ?>/images/logo.png" alt="Vaandel Uitgewers.co.za" /></a>
    					</div>
    				</div>
    </div>
    </div>
    </header>
    Moderator Andrew Nevins

    @anevins

    Forum moderator

    So which part of your code

    if ( is_front_page() ) :
      get_header('header-home.php');
    elseif ( is_404() ) :
      get_header('404');
    else :
      get_header('header-site.php');
    endif;

    Isn’t working?

    I have NO idea what to do with that var_dump. The link to the php.net manual made no sense to me, or I am brain dead lazy today.

    All I need to do is:
    1) Display Header A on Homepage
    2) Display Header B on EVERY other page.

    So I created two header files for the job and according to some sources I need to place a bit of code somewhere in my header.php. That code will reference to a certain header depending on what the page is.

    Thus:
    If (is_front_page) then load header-home.php otherwise…load header-site.php.

    The only catch is that my header has some Javascript. I tried loading it up in the header.php but that didn’t work either.

    My code is in the right place?

    You know what. I just thought of something.

    Why not load up the Javascript and relvant Div’s for the header structure right after the if(is_front_page and else in the main header.php. then I won’t need the extra files.

    esmi

    @esmi

    Forum Moderator

    Try changing <?php get_header();?> to:

    <?php
    if( is_front_page() || is_home() ) get_header( 'home');
    else get_header( 'site' );?>

    It worked!

    Well, kind of. My central logo is missing, but everything else looks fine.

    I’m getting closer 🙂

    Thanks esmi

    I will try that in a moment…

    esmi

    @esmi

    Forum Moderator

    You’d also be better off enqueueing your javascripts. Not only is that better practice but you can also enqueue them conditionally if you need to.

    Okay. So everyhting works fine.

    Just to recap:

    All I did was place this short code between my wrapper div and before my <nav> section:

    <?php
    if ( is_front_page() ) {?>
    <header >
    <div id=""> Some header div's and <img>, blah blah, blah </div>
    </header><?php
    }
    
    else {?>
    <header >
    <div id=""> Some OTHER header div's and <img>, blah blah, blah </div>
    </header><?php
    <?php }
    ?>

    And wallah! I dunno if this is more of a hack but it works simple and great and I only need one header.php file – the original. 🙂 awesome.

    I could probably mark this as solved except if someone wants to warn me about something… hope not.

    Thanks for that tip esmi. I will look into it.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Load different header on Homepage’ is closed to new replies.
Skip to toolbar