In this post I explain the advantage and purpose of template parts and the use of the WordPress function get_template_part , which is used to load template files in the WordPress theme.
A few weeks ago I wrote a beginners tutorial about the different template files in a WordPress theme . I already mentioned there that theme developers can split template files into individual so-called template parts.
These template parts are loaded with the get_template_part () function.
Advantage and purpose of template parts
Template parts are particularly useful because they can be used multiple times in different places in the theme.
Blog posts are displayed, for example, on the start page, in categories, author archives or in the search results. Instead of having to incorporate and maintain the same code in four different files home.php, category.php , author.php and search.php , it can be stored in a single template file content.php .
This avoids repetitions and redundancies, true to the motto:
Don't repeat yourself!
Why get_template_part () should be used
Anyone familiar with PHP knows that include and require are two functions for loading PHP files in the programming language. As a WordPress theme developer, you will also use this to load additional function files in the functions.php of the theme.
However, WordPress provides a number of its own functions for integrating template files , including get_template_part ().
The reason for this is the expandability through child themes and plugins!
The functions first check whether the loaded file is available in a child theme and can be loaded from there, or alternatively the template of the parent theme is used. Template files can only be overwritten by using these functions with a child theme.
In addition, most functions contain additional action hooks, which means that plugins can intervene in the loading of the template files.
As a theme developer, you should therefore always use the WordPress functions instead of include and require to include template files in order to ensure the expandability of your theme with child themes and plugins.
Load template files with get_template_part ()
The function takes two parameters:
get_template_part( $slug, $name );
The first parameter must be passed, the second is optional and is used to select an even more specific template file.
Load general template file
In order to integrate the template file content.php - for example within the WordPress loop - it is sufficient to call the function and transfer the slug content :
<?php get_template_part( 'content' ); ?>
The content.php then takes over the presentation of the contribution and can be loaded in several templates, e.g. the archive.php , index.php and search.php .
Load special template file
The second parameter can be passed to load a special template file.
This can be used, for example, to call up another file to display an article in the individual view.
<?php get_template_part( 'content', 'single' ); ?>
WordPress now tries to load the more specific template content-single.php by specifying the second parameter. If this is not available, the general template content.php is used as a fallback.
The loading sequence is therefore as follows:
Because of this fallback mechanism, the function also accepts two parameters.
It would therefore not be entirely correct:
<?php get_template_part( 'content-single' ); ?>
Load template files variably
A variable or function can also be transferred as a second parameter.
In TwentySeventeen, for example, the get_post_format () function is used to integrate a special template for the respective post format:
/* Start the Loop */ while ( have_posts() ) : the_post(); get_template_part( 'content', get_post_format() ); endwhile;
Depending on the post format, WordPress will try to load content-audio.php, content-gallery.php, content-image.php etc. If template files do not exist, content.php is used.
Load template files into folders
Very often you will want to store template files in sub-folders in the theme instead of inflating the theme's root directory with them.
To do this, the folder name can simply be put in front of the file name:
<?php get_template_part( 'template-parts/content', 'single' ); ?>
Now the file content-single.php is loaded from the folder template-parts .