WordPress.org

Ready to get started?Download WordPress

Forums

Load different header on Homepage (12 posts)

  1. Gustavius
    Member
    Posted 1 year ago #

    [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! :)

  2. Andrew
    Forum Moderator
    Posted 1 year ago #

    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

  3. Gustavius
    Member
    Posted 1 year ago #

    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>
  4. Andrew
    Forum Moderator
    Posted 1 year ago #

    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?

  5. Gustavius
    Member
    Posted 1 year ago #

    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?

  6. Gustavius
    Member
    Posted 1 year ago #

    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.

  7. esmi
    Forum Moderator
    Posted 1 year ago #

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

    <?php
    if( is_front_page() || is_home() ) get_header( 'home');
    else get_header( 'site' );?>
  8. Gustavius
    Member
    Posted 1 year ago #

    It worked!

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

    I'm getting closer :)

  9. Gustavius
    Member
    Posted 1 year ago #

    Thanks esmi

    I will try that in a moment...

  10. esmi
    Forum Moderator
    Posted 1 year ago #

    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.

  11. Gustavius
    Member
    Posted 1 year ago #

    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.

  12. Gustavius
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic