How to make get_template_part always check child theme first?

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.

Read More

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.

Related posts

Leave a Reply

2 comments

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

    EDIT:

    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().