I can find lots of "how" answers in reference to using get_template_part()
, but no "why" except that it can cut down the amount of code to be written. I'm asking this from an end user's perspective, where I'm tweaking within a child theme. My interest is in possible performance related issues once a site is up and running.
My chosen master theme uses this method a lot, using and sometimes nesting several within each base template. It's complicated to keep track of whether tweaking a template part for one purpose will also have an effect elsewhere. I have several custom post types and a lot of taxonomies, and most need to be displayed differently. I therefore want to replace get_template_part()
with the full code in many instances, for control without forgotten ramifications.
However, before I go down this path I thought I should check I'm not missing something. For instance, apart from the amount of code-writing, once complete does it have significant effects on memory? Or page loading speed? Or something else I haven't even thought of?
I can find lots of "how" answers in reference to using get_template_part()
, but no "why" except that it can cut down the amount of code to be written. I'm asking this from an end user's perspective, where I'm tweaking within a child theme. My interest is in possible performance related issues once a site is up and running.
My chosen master theme uses this method a lot, using and sometimes nesting several within each base template. It's complicated to keep track of whether tweaking a template part for one purpose will also have an effect elsewhere. I have several custom post types and a lot of taxonomies, and most need to be displayed differently. I therefore want to replace get_template_part()
with the full code in many instances, for control without forgotten ramifications.
However, before I go down this path I thought I should check I'm not missing something. For instance, apart from the amount of code-writing, once complete does it have significant effects on memory? Or page loading speed? Or something else I haven't even thought of?
Share Improve this question edited Feb 21, 2020 at 8:26 Kevin4fm asked Feb 21, 2020 at 7:45 Kevin4fmKevin4fm 7110 bronze badges 3- 1 In general, I don't think it has an impact on performance, provided your code is good. The primary downside for NOT doing this is that if you want to make a change, you have to go make it in every single template instead of in one spot. And then you might miss something, or forget a spot. Save yourself some sanity and use the template parts. – Faye Commented Feb 21, 2020 at 23:52
- Thanks Faye. Perhaps I need to find a happy medium where I'm using the benefits of template parts where it's logical - e.g. in single posts with slightly different formats - without the complexity of trying to use the same template for categories and tags and posts, singular content and summaries, etc. which my current theme seems to be doing. – Kevin4fm Commented Feb 22, 2020 at 8:16
- Certainly, there's no reason you shouldn't create separate templates if you need them to behave or look differently. If the changes are minor, you should be able to add conditional php statements to the reusable parts that get you where you're going. – Faye Commented Feb 22, 2020 at 18:26
1 Answer
Reset to default 0Main - Loads a template part into a template.
The function provides a mechanism for child themes to overload reusable sections of code in the theme. So you have always the right path, file and a solid chance for developers, other code to change the result. See also this solid answer to a similar question.
But the magic behind get_template_part() is in the function locate_template(), which checks first the theme directory, then the parent directory (if one exists) for the file named. This is very useful for plugin development.