Any good theme or plugin developer would break the theme files into small ones and then included them as and when needed. This way you you save yourself from the cumbersome job of editing each and every php file. But, how you do you include the php file, the correct way in wordpress.
If you remember, using the native php code, you will include the php files like this..
include 'myfile.php'; include_once 'my file.php'; require('my file.php');
In wordpress, you include the files, safely using get_template_part() function. This is the correct way of doing it. The difference is with the include, if the file is not present, php crashes the whole site and dies, which is not in the case of above function. Lets see how we use this function.
To include, the header, footer, comments files, this is hard coded..
- get_header() => Includes header.php
- get_footer() => Includes footer.php
- get_sidebar() => Includes sidebar.php
- get_search_form() => Includes searchform.php
- comments_template() => Includes comments.php
The get_template_part() comes in handy, to include your custom php files like this.
get_template_part('navigation') => Includes navigation.php get_template_part('include/pagination.php') => Includes pagination.php under the include folder get_template_part('navigation','2') => Includes navigation-2.php
Notice how 2 parts of filename are included in line 3.
Pretty easy! isn’t it!