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?

  • 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 Feb 21 '20 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 Feb 22 '20 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 Feb 22 '20 at 18:26

Main - 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.

  • Thanks Bueltge. I meant why from an end user's perspective, not a theme developer's. I'll amend the text to make this clearer. I'm already using it in a child theme and it's not going to need overwriting by others. I'll be overwriting the main templates rather than the template parts it calls because there will likely be effects elsewhere if I do that. I'm trying to find out if there's any downside in doing that other than more code writing for me. – Kevin4fm Feb 21 '20 at 8:16
  • It is not clear for my why should an end user have interest on code functions and his benefits? – bueltge Feb 24 '20 at 15:50
  • In this case I am the end-user. I know enough WordPress code and PHP to recode parts of the child theme to my purpose. However, the theme I've chosen uses get_template_part extensively, to a degree that is confusing and difficult to keep track of. I want to simplify some templates so I know that changes I make in one place won't appear elsewhere as well, where it might not be appropriate. My concern/question is whether by doing so there are downsides other than the amount of code to be written, e.g. in a greater page load time. – Kevin4fm Feb 24 '20 at 19:59
  • If you remove this function you should get no benefits in speed. Especially if you install use caching mechanism on the server and this central function would cache via server. I would recommend using them, because the points on my answer. – bueltge Feb 27 '20 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.