WordPress templates are a critical part of WordPress development, and using them correctly can make website development a lot easier. In this article, we’ll focus on one of the most important features of WordPress templates – the get_template_part() function.

What is get_template_part() in WordPress

The get_template_part() function is a WordPress template tag that allows developers to include template parts in their WordPress themes.

Template parts are reusable parts of a theme that can be used on multiple pages or sections of a website. For example, a header, footer, sidebar, or any other blog section are commonly used template parts.

Using the get_template_part() function allows you to keep your code organized and improve readability and maintainability. Instead of repeating the same code on multiple pages, you can create a template part and reuse it throughout your theme.

To use the get_template_part() function, you’ll need to add a few lines of code to your theme files. So the first thing you’ll need to do is create a new file in your theme directory. The file should be named after the template part you’re creating, and it should end with .php

WordPress Template Parts: What Are They?

A template part in WordPress is a small piece of code that can be reused across multiple templates. It allows you to keep your code DRY (Don’t Repeat Yourself), making your theme more maintainable and easier to read.

Developers and designers create template parts to help streamline the process of building WordPress themes. Instead of having to rewrite the same code over and over again for each template file, you can create a single template part and reuse it in multiple places. This can save you a lot of time and effort, especially when building complex WordPress themes.

Template parts are typically used for common elements such as headers, footers, navigation menus, sidebars, and content areas. By creating a template part for each of these elements, you can easily swap them out or modify them as needed without having to edit every template file on your website.

How Do WordPress Template Parts Work?

WordPress template parts work by allowing you to create a reusable block of code that can be called in multiple places. When you create a template part, you give it a unique name and store it in the /wp-content/themes/your-theme-name/ directory of your WordPress installation.

Once you have created a template part, you can call it in any of your template files using the get_template_part() function. This function takes two arguments: the first is the name of the template part you want to call, and the second is an optional parameter that allows you to pass data to the template part.

Let’s See How We Can Use get_template_part() With WordPress

Calling get_template_part() in WordPress

To call the get_template_part() WordPress function, you must first create the file you wish to call.

The function acts similar to the require() PHP function, helping to save on development time by not having to repeat your code multiple times in separate files.

get_template_part( 'file-to-fetch' );

This above code will fetch file-to-fetch.php within your active WordPress theme.

get_template_part( 'fallback-file', 'main-file' );

This above code will fetch fallback-file-main-file.php and if that doesn’t exist, it will fetch fallback-file.php.

get_template_part( 'content', get_post_format() );

This above code will fetch content-postformat.php for example.

Getting Template Parts in Folders

To get template parts in folders, use the code below.

get_template_part( 'example-folder/example-file' );

This above code will fetch your-theme-folder/example-folder/example-file.php.

Using get_template_part() For Child Themes

One of the benefits of using the get_template_part() function is that it allows you to override template parts in child themes. If you create a child theme and want to modify a template part, you can create a new file with the same name in your child theme directory.

When WordPress looks for the template part, it will first look in the child theme directory, and if it’s not found, it will use the parent theme’s template part.

get_template_part( 'fallback-file', 'main-file' );

To use this function with child themes, please see the breakdown of file priorities below.

  1. child-theme/fallback-file-main-file.php
  2. main-theme/fallback-file-main-file.php
  3. child-theme/fallback-file.php
  4. main-theme/fallback-file.php

Let Me Explain You to Another Way: What get_template_part() Is in WordPress

get_template_part() is a WordPress function that includes template “partials” wherever you need them. This allows you to centralize any block of code that is likely to be repeated into these “partials,” cutting down on duplication and making your template files more readable. Effective, it tells WordPress to load template parts, like the body of a post in different theme files (which are usually part of the template hierarchy).

Let’s take an example loop in an index.php file, using get_template_part():

/* Environment: while loop in index.php */
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        get_template_part( 'content' );
    endwhile;
endif;

And here’s our example content.php:

/* Environment: We're in content.php */

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h2 class="entry-title">
	    <a href="<?php the_permalink(); ?>">
	    	<?php the_title(); ?>
	    </a>
	</h2>

    <?php the_excerpt(); ?>
</article>

In this example, WordPress will load up index.php, and it will run as if the contents of content.php were inside it. You can think of it as being WordPress loading in the “sub-template” completely in the line where get_template_part() was called. This lets us avoid rewriting (or maintaining) all the same code around the various files of our WordPress theme.

Conclusion

In conclusion, using the get_template_part() function is an excellent way to keep your WordPress theme organized and easy to maintain. It allows you to reuse code and keep your theme files DRY (Don’t Repeat Yourself).

If you’re new to WordPress development, taking the time to learn about the get_template_part() function will make your development process smoother and more enjoyable. WordPress’s get_template_part() is a powerful and valuable part of WordPress theme development. You’ll see it in most good themes you try to edit, so I hope you now know what it’s doing and why it’s valuable.