How can I force WP to always check the child theme folder first when running get_template_part?

Example: child theme loads get_template_part('content', 'inventory') in single.php. Because all child themes (and there are a lot) share the same common inventory template base, the file content-inventory.php is in the parent theme. So far so good.

I would like to add a small section to said inventory template that will be unique to each child theme. Adding get_template_part('content', 'inventory-special') into content-inventory.php will not check the child theme directory first.

  • 3
    To the two of you who down-voted my answer and did not comment as to why, thanks for nothing. How is someone suppose to improve themselves with negative criticism? – m-torin Jun 19 '11 at 20:24

It does, by default. The get_template_part() function uses locate_template() which cascades through the template files in in order of specificity and stylesheetpath/templatepath.

So, if your Child Theme includes a content-inventory.php, then get_template_part() will include it; if not, then it will look for content-inventory.php in the parent Theme. If it doesn't find it, it will then look for content.php first in the child, then in the parent.


Taking a stab at understanding what you mean; please clarify if I'm misunderstanding...

You want to include a new template part file within a Parent-Theme template file called content-inventory.php, right?

The only way, AFAIK, that you can do that is to copy content-inventory.php into your Child Theme, and then add the new get_template_part() call where needed.

If a file named content-inventory.php is included in both the Parent and the Child Theme, then WordPress will always use the Child Theme version, if included using get_template_part().

| improve this answer | |
  • @Kaiser, @Chip Bennett: WP will obviously check the parent theme for content-inventory.php. The Codex and simple experience makes that clear. However if the file being sought (level 2) is in the parent theme folder, WP does not recheck the child theme for a get_template_part contained within the file being sought (a third-tier include). Has this worked differently for you? – m-torin Jun 19 '11 at 20:17
  • I don't understand your question/problem. For any given template file name, WordPress always checks the Child Theme first. Can you explain what you mean by second- and third-tier files? – Chip Bennett Jun 19 '11 at 20:37

get_template_part() does this per default.

| improve this answer | |

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.