How to use the get_template_part() function in WordPress: A Comprehensive Guide

Your website’s appearance as well as its functionality may be modified and improved in a variety of different ways using the powerful content management system that is WordPress. Among the various features, the get_template_part() function stands out as a useful tool to create dynamic and modular templates. In this article, we’ll go deeply into the WordPress get_template_part() function, discussing its many uses as well as the ways in which you may optimise your WordPress development workflow by making use of it.

What is the get_template_part() function?

The get_template_part() function is a native WordPress function (since its introduction in version 3.0) that allows developers to include reusable parts of code, called template parts, within a theme or child theme. By using this function, you can break your theme’s template files into smaller, more manageable components, making it easier to maintain, update, and organise your code.

Benefits of using get_template_part()

  1. Code reusability: Make use of a singular template part in a number of different locations across your theme to cut down on the amount of duplicate code and make it simpler to manage and upgrade.
  2. Modularity: You’ll end up with cleaner and more organised code if you split up your templates into components that are more manageable in size and scope.
  3. Child theme compatibility: It makes it easier to create child themes since it enables you to override template portions in the child theme without having to make any changes to the parent theme.

Why not use a PHP include?

Good question. For a long time (pre WordPress 3.0) this is how it was done in WordPress, but while both get_template_part() and PHP’s include function serve the purpose of including external files in your code, there are key differences that make get_template_part() more suitable for WordPress theme development. The get_template_part() function not only allows for better organisation and modularity by including template parts based on slug and name, but it also seamlessly supports child theme overrides. In contrast, the include function lacks these features and requires specifying the exact file path, making it less flexible for WordPress theme development.

Basic syntax of get_template_part()

The basic syntax of the get_template_part() function is as follows:

get_template_part( string $slug, string $name = null );

$slug (required): The base identifier for the template part.

$name (optional): The modifier for the template part, allowing you to create variations of the same base template part.

Example Usage

Example 1: Basic usage

Suppose you have a template part called “content.php” within your theme’s folder. You can include this template part in any other template file, like “index.php” or “single.php”, using the following code:

get_template_part('content');

This code will search for “content.php” in your theme’s directory and include it in the specified location.

Example 2: Using folders

You can include your template parts within folders for neater organisation.

get_template_part('partials/content');

This would look for a file called “content.php” within a directory called “partials”.

Example 3: Using a named template part

Let’s say you have two template parts for displaying content, one for articles and one for pages. You can create two template parts, “content-article.php” and “content-page.php”, and use the following code to include them:

// In a loop for articles
get_template_part('content', 'article');

// In a loop for pages
get_template_part('content', 'page');

Example 4: Overriding template parts in child themes

Assume you have a parent theme called “mytheme” and a child theme called “mytheme-child”. If you want to override a template part, like “content.php”, create a new file called “content.php” in the “mytheme-child” directory and use the following code in your template files:

get_template_part('content');

WordPress will automatically use the “content.php” from the child theme if it exists; otherwise, it will fall back to the parent theme’s “content.php”.

Let’s wrap it up

The get_template_part() function is an indispensable tool for WordPress developers looking to create modular, maintainable, and organized themes. By mastering this function, you’ll be able to streamline your development process and create more efficient themes for your projects. So, go ahead and harness the power of get_template_part() to enhance your WordPress development experience!