I have 3 theme files:
They both have content which is added to via a
navigation.php has some content added to it via a
Based on the page being called, I would want to include
header.php as well.
How would I do this?
navigation.php to be included into
page.php while both have content that is added to it via a
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 would use some function to include
navigation.php based on
If that idea is right, can someone please elaborate and give me details on what functions I would use, etc?
You can include any template you want using
<?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
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.
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.