No Cowboy Coding: Get Template Part

WordPress has a useful function that gets a template part

You might have ran into a problem when developing a child theme, or at times, the code can get a little messy. This is where WordPress’s get_template_part() function can come in handy. Instead of having to have the WordPress Loop in five different files, you can have it all in one. You can take a look at get_template_part’s Codex Page to learn more. Konstantin Kovshenin‘s get_template_part post also demonstrates on how useful this function can be.

The Function

The function is rather simple:

<?php get_template_part( $slug, $name ); ?>
  • $slug: The slug name of the template    (required)
  • $name: The name of a specific template (optional)

The two parameters might seem consfusing at first, but let’s create a hypothetical situation. Let’s says that you have a custom WordPress loop in your index.php, single.php and archive.php files. This can get messy and if you need to make a change to the index.php and single.php file’s loops, then it can be tedious. You could create a file called loop-content.php and have all of the loops in one place. That will save a lot of time! Not only that, but if you are working on a child theme, it will speed up your development tenfold.

To call the template file, the function will look like this

<?php get_template_part( 'loop', 'content' ); ?>

The get_template_part function will look for files in the following order:

  1. wp-content/themes/twentyfourteenchild/loop-content.php
  2. wp-content/themes/twentyfourteen/loop-content.php
  3. wp-content/themes/twentyfourteenchild/loop.php
  4. wp-content/themes/twentyfourteen/loop.php

As you will notice, the function will look for the specified file in the child theme first. If it does not find the file, then it will look in the parent theme. It is important to remember the order that the function uses to find the files, especially for debugging.

You can also use the is_home() and the is_archive(), is_single() functions in your loop-content.php template file.

Konstantin has gives some advice:

Although get_template_part is similar to a PHP include or require, you should not use it to include things like your theme options code, sidebars registration, custom widgets, etc. The get_template_part function should only be used to get template parts.

Konstantin Kovshenin on get_template_part

But what if you have the template in a subdirectory? Thankfully, it’s as simple as adding the following folder name followed by a slash. For example:

<?php get_template_part( 'templates/loop', 'content' ); ?>

If you can take away one thing from this post, it’s that it is very important to make your files readable. This will make it easier for co-workers or others to modify, read and change your files. Using the get_template_part can make everyone’s life a whole lot easier. You can also use the function to reuse your code several times which makes the function incredibly useful. In fact, I would start using it as soon as possible!

If you have any questions, please leave a comment below. Thanks for reading!