0

I am building a site using a custom WP theme, and I have been using:

<?php include('header-bar.php'); ?> 

in order to include a navigation bar on each page. I intentionally didn't put the navigation in the header.php file because I don't need it on every page.

That bit of code is working fine in certain places, but failing in others.

It works in this scenario:

<?php get_header(); ?>


  <div class='main-content'>

  <?php include('header-bar.php'); ?>

    <div class='container'>

            <?php 

                if ( have_posts() ) : while ( have_posts() ) : the_post();

                    get_template_part( 'content', get_post_format() );

                endwhile; endif; 
            ?>
    </div>
</div>


<?php get_footer(); ?>

but it mysteriously fails in this scenario:

<?php 



/*
Template Name: Contact
*/


?>

<?php get_header(); ?>


<div class='main-content'>

  <?php include('header-bar.php'); ?>

    <div class='container'>

            <?php 

                if ( have_posts() ) : while ( have_posts() ) : the_post();

                    get_template_part( 'content', get_post_format() );

                endwhile; endif; 
            ?>

    </div>
</div>


<?php get_footer(); ?>

In this second scenario, the page displays properly, except for a the 'header-bar.php' code which is nowhere to be found. The change that I am making that seemingly 'breaks' this line of code is by adding the template name. Can anyone offer some insight into why this may be? I'm baffled.

Thanks in advance! CPR

2

Hello CandyPaintedRIMS,

Please try get_template_part() function to include any file into your custom template.

Your code:

<?php include('header-bar.php'); ?>

It should be replaced with:

<?php get_template_part( 'header-bar' ); // include header-bar.php ?>

Hope this will helps you.

Thank you

2
  • Yes it did work! Thank you. Do you have any clue why include would work in the other scenarios, but I have to use get_template_part in this one? – CandyPaintedRIMS Dec 26 '16 at 7:52
  • @CandyPaintedRIMS in the top comment block you have Template Name: Contact which indicates, that you are in a child theme of Contact theme. If the file you are including is not in the child but in the parent theme, simply using include() won't work because it only checks the current working dir and include paths. get_template_part() on the other hand is aware of the theme hierarchy and checks the parent theme folder too. – JHoffmann Dec 27 '16 at 10:32
1

For the header, you should use the get_header function.

Your code:

<?php include('header-bar.php'); ?>

Should be replace with:

<?php get_header( 'bar' ); ?>

The get_header function uses locate_template which:

Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat so that themes which inherit from a parent theme can just overload one file.

and also has a fallback to use the default header.php file in any situation the specified header can't be found.

1
  • The OP's code shows that header.php is already being included before header-bar.php is to be included. So falling back to header.php and include it a second time is probably not expected behaviour. Therefore I would go with AddWeb's answer. – JHoffmann Dec 27 '16 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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