Some WordPress theme developers neglect to use the get_template_part() function when writing code for their templates or themes, despite its great importance in organizing files and reducing a lot of duplicate code in template files.
In today’s article, I will explain more about this function and explain its general structure, the features it offers, and its important role in enhancing the WordPress theme structure and making codes shorter and simpler.
How to call template parts (template files) in WordPress
WordPress automatically recognizes sample template files as full-page template files, and automatically uses or calls them in the appropriate place based on the WordPress theme file hierarchy. So when we create a full page template file in your WordPress theme you don’t have to call it from another file.
Read more: Learn about the basic structure and hierarchy of WordPress themes
But what about the small parts of the template that do not represent complete pages, but rather are files dedicated to displaying specific parts of the page, such as comments.php, which is responsible for displaying comments, header.php, which is responsible for displaying the header, and footer.php, which is responsible for displaying the footer and sidebar. php responsible for displaying sidebar etc…
These files include custom code to display specific parts to be displayed within other template files. In this case, you need to call up the required template part in the position you need yourself. How can this be done?
Here we must differentiate between the general parts and the private parts of the template or theme. As for the general parts that are used in several places within the site, such as (header, footer, sidebar, and search form), each of these parts is included through its own specific function in WordPress, such as:
- get_header() calls the header.php file that forms the top of the site’s pages.
- get_footer() : Calls the footer.php file. Which usually displays the site’s footer.
- get_sidebar() : Calls the sidebar.php file responsible for displaying the sidebar of website pages.
- get_search_form(): Calls the searchform.php file if it is located in the home directory of your current template or within the parent template of this template. Otherwise, it will call the WordPress default search engine template, which is an HTML form.
Now what about the private parts of the template, that is, what if you want to write your own files that represent parts written using your own code within the template, these parts that WordPress does not automatically recognize through the template hierarchy, and also do not have custom functions to call them? How can you call these files multiple times, or call them when certain conditions are met according to each template?
For example, suppose you manage to create a template file that includes custom code to display the sidebar on your home page differently from the rest of the pages, or write specific code to display the logo that is used in more than one place and called in different template files? To do this, you need to use a function called get_template_part() to call these custom parts.
What is the function get_template_part
The get_template_part() function is a pre-made script function that WordPress provides to theme developers that allows them to load reusable sections of code into different locations of their custom themes.
In order to use this function, you must first create a template file that includes your code that you want to use multiple times within the theme, for example custom-part.php, then pass the file name to this function (without the php extension) and call it. Anywhere you want in the WordPress theme as follows:
This function is similar to PHP’s include() function , so you can include the same template fragment multiple times in your template.
The get_template_part function takes the following general form:
As you can see from the code above, the function can take 3 parameters, which are as follows:
- $slug: An arbitrary parameter that is a string representing the generic name of the generic template file.
- $name: An optional parameter whose default value is null. It represents a string representing the name of the specialized template.
- $args: An optional parameter representing an empty array into which we can pass additional parameters to the template.
For additional examples of passing parameters to a function, we have the following code that fetches the file
fallback-content-main-content.php (a more specific file)
and if it does not find it, it fetches the file fallback-content.php (a more general file):
The following code fetches the file content-postformat.php
It is preferable to collect all the duplicate codes that you want to call through the get_post_format() function in the form of files, and place them in a separate folder inside the template folder and call it, for example, parts, or template-parts, or includes, or any expressive name you want, and then write the folder name before the file name. as follows:
What are the benefits of using get_template_part() function?
- When using the get_template_part function, the amount of code a developer needs to write when developing their theme will be reduced.
- Another important thing that the get_template_part function is useful for is the ease of making modifications in one of the parts of the template. If you want to make a change in a specific part, it is enough to do it in one place instead of doing it in more than one place within the template files.
- Finally, the get_template_part function is useful in developing a child theme, as it provides a simple mechanism for child themes to overload reusable code fragments into the parent theme.
For example, if we include a file called nav.php using get_template_part() in both the parent and child themes, WordPress will initially look for the child theme version and use it first, and if it does not find it, it will then use the parent theme version.
Since the function initially searches for the required part of the code in the child template folder of the current template, and if it does not find it, it searches in the parent template. This mechanism enables you to partially bypass the parent template easily by including the codes that you want to modify in the child template.
note:
When creating a child template using the get_template_part function, you must pay attention to the priorities of calling the subtemplate files for the child and parent templates, as they are done in WordPress as follows:
- In this case, the file fallback-content-main-content.php for the child template will be searched first
- Then look for the file fallback-content-main-content.php for the parent template
- Then look for the file fallback-content.php for the child template
- Then look for the file fallback-content.php for the parent template
Read more: How to create a Child theme in WordPress to enjoy its benefits
Practical examples of using the get_template_part() function
In order to further understand the importance of this function, let us explain some practical examples of using this function in a WordPress theme
Example 1: Encapsulating nav.php navigation menu code
If you need to call the wp_nav_menu function to display the main navigation menu of the site in more than one of the template files, in this case all you have to do is record the position of the primary menu in the functions.php file , then create a new file and name it nav.php for example and write the following code inside it:
Read more: Create navigation menus in a WordPress theme and customize them with code
Now you can call this file in any other template file. For example, to display the main menu in the site header, we write the following in the header.php file:
When this function is called in the header.php file, the function will search for the presence of a file named nav.php in the current template folder and display its content. If the file is not available, executing this function will not return any results, nor will it cause any error in the code.
Then, if you need to display this main menu in the footer of the site as well, all you have to do is write the same line of code as before and call the get_template_part function in the appropriate position within the file footer.php instead of writing the entire code to display the menu over and over again..
Example 2: Encapsulating the WordPress loop code loop.php
When you develop a template for a blog, you will need to write WordPress loop code to display the site’s articles several times in different template files within the template, which is considered one of the main repetitive codes when developing any WordPress template.
In this case, you can write the standard WordPress loop code in a separate file, name it loop.php, and call it anywhere you want within your theme by writing a single line of code instead of repeating the same code in more than one file, as follows:
If you also want to write WordPress loop code to display articles on single article pages or on archive pages in a different way than the way they are displayed on the home page, in this case all you have to do is create different files that allocate the general loop display code loop.php and call them in the desired location with Pass additional parameters.
For example, you can create a file loop-single.php with the required code and call it within the template template file single.php as follows:
Create a file called loop-archive.php dedicated to calling the loop code on the archive.php template page through the following line of code:
The default Twenty Ten template in WordPress works in this way, as shown in the following image, which shows part of the template’s file structure. You can examine all files that begin with the word loop for further clarification.
Conclusion
Here we have reached the end of today’s article in the WordPress template development series. In it, we learned about the get_template_part() function, which allows template developers to organize and arrange their template files. We explained how this function can be called correctly and save time and effort in developing and maintaining the template.
If you are a WordPress template developer, and you find that you are writing duplicate and similar code in more than one place in your template files, I strongly advise you to collect these parts into separate files, and call them through this function to make your template code easier, cleaner, more concise, and more organized.
Leave a Reply