The Correct Way To Load A Template File In WordPress

Since WordPress 3.0 there has been a funcion called get_template_part(), which has been used quite a bit in the new Twenty Ten default WordPress theme. This is an evolution of the usual way to include parts of themes by using functions like get_header() to include the header.php file. This function helps code resuse by including the same files multiple files, but also allows child themes to override parent themes.

The function takes a required slug and an optional name as parameters, the two parameters being used together to select the correct template file to include. The best way to describe how this function works is to show how it would work in certain situations. Take the following call to get_template_part() using only the slug parameter.

get_template_part('foo');

This will look in your theme directory for a file called foo.php and load it into the template where the function call is placed. If you are using a child theme then it will look in the child theme directory first and the parent theme directory second. The upshot of this is that you can override portions of your parent theme without having to copy lots of files around, just create the needed files in your child theme and this function will load them first.

The second parameter (called name) is used to define a more specific template file that you want to grab hold of. The following call to get_template_part() uses both the slug and name parameters.

get_template_part('foo', 'bar');

This first looks for a file called foo-bar.php in the current theme directory, and if this doesn't exist then it looks for a file called foo.php. If the current theme is a child theme then it will look for the files in that order in the child directory before looking in the same order in the parent directory. For clarity the files are looked for in the following order.

  1. /wp-content/themes/themechild/foo-bar.php
  2. /wp-content/themes/themechild/foo.php
  3. /wp-content/themes/themeparent/foo-bar.php
  4. /wp-content/themes/themeparent/foo.php

Another use of this function is to create multiple parts for the same section of your theme. For example, you might want to have different ways of printing out the content of the page currently being looked at, in which case you might want to do something like this.

if (is_singular()) {
    get_template_part('single');
} else {
    get_template_part('multi');
}

In this case, if the page being looked at is a single page (eg, a single post or page) then single.php is loaded, otherwise multi.php is loaded.

One of the most useful things about this function is that it means you don't have to use include() or require() function calls in your theme code, but additionally, you can allow people to use your theme as a parent theme and apply their own custom parts without having to edit your theme.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
4 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.