1

When i use the "get_template_part();" inside a loop, does it search for that template file every cycle of the loop (each post) or does it search for the file once and then reuse it every cycle of the loop?

  • Are you asking if php has to load from disk every cycle of your loop? I'd expect not. Many themes include parts within their loops. I've never found a performance hit from this. – Andy Macaulay-Brook Jul 28 '16 at 21:30
2

get_template_part() calls locate_template(), and both functions are running file_exists() checks for the same file over and over. There is no caching. Meh.

But … PHP has an internal cache for file look-ups, so a direct file access will not happen on every call.

There is an edge case, most developers aren't aware of: A file can be deleted after the first access, and file_exists() will still return true if PHP's cache hasn't updated.

  • So if i except a large number of loops on one page should i just insert the code? – Ben Jul 29 '16 at 13:09
  • Or call locate_template() once and save the value for reuse. – fuxia Jul 29 '16 at 13:10
2

That depends entirely on your code. Without seeing any, i will assume you are using a standard WP loop. If so, the template part is included every time the statement is executed. By default that is for every post in the loop.

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.