How do I include a template file while allowing it to render its own dynamic content added via custom box?

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?

Solutions Collecting From Web of "How do I include a template file while allowing it to render its own dynamic content added via custom box?"

You can include any template you want using get_template_part()


<?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.:


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.