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.
It does, by default. The
get_template_part()
function useslocate_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
, thenget_template_part()
will include it; if not, then it will look forcontent-inventory.php
in the parent Theme. If it doesn’t find it, it will then look forcontent.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 newget_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 usingget_template_part()
.get_template_part()
does this per default.