Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have 3 theme files: header.php, navigation.php and page.php

page.php calls header.php They both have content which is added to via a custom box.

navigation.php has some content added to it via a custom box. Based on the page being called, I would want to include navigation.php within header.php as well.

How would I do this?

I want navigation.php to be included into page.php while both have content that is added to it via a custom box.

My guess Create a page called 'Navigation', for example. I never display this link in my templates so that it's not used as a page on it's own even though it exists as one. This will allow me to edit its contents.

Create another page by whatever name, this would use page.php. page.php would use some function to include navigation.php based on is_home().

If that idea is right, can someone please elaborate and give me details on what functions I would use, etc?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can include any template you want using get_template_part()

e.g.:

<?php get_template_part('navigation'); ?>

Using this function also gives you child/parent theme support.

Also of note, you could put:

<?php get_template_part('navigation','search'); ?>

Which would attempt to load navigation-search.php, but if that isn't found or doesn't exist, it will fallback and load navigation.php instead.

I use this in my personal code to make loading different excerpts for different post types easier, e.g.:

get_template_part('excerpt',$post->post_type);

It will load excerpt.php by default, but I could override it for any post type I wish e.g. excerpt-page.php

As a sidenote, use this function only for template files, do not use it as a general purpose include statement, e.g. it would be a bad idea to load a PHP library or a configuration file this way.

share|improve this answer
    
Perfect. Thanks a lot. That really helped :) –  Housni Apr 17 '12 at 14:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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