So I've the following code in order to search for templates inside a custom theme I'm working on:

<?php $my_theme = wp_get_theme();
get_template_part( 'masthead',  $my_theme->Template); ?>

Basically what it does, is that it does a search for a file called "masthead-themename.php". If that file does not exists, then it looks for a file called "masthead.php". This works as expected.

However, I would to have something that searches for the file inside of a folder called "templates" as a first option and if not, then search for the template name in the theme's root folder.

I tried this

<?php $my_theme = wp_get_theme();
get_template_part( 'templates/masthead',  $my_theme->Template); ?>

But this only works if the file exists in the template's folder. If the file exists in the root's theme folder, it does not recognize it.

Any ideas?


1 Answer 1


I assume you have a file in templates/masterhead-templatename.php, and it works, it is ok.

Correct me if I am wrong, you want to locate this file like this masterhead-templatename.php, basically one folder up, but this wordpress function does not work like this.

If the file exists in the root's theme folder, it does not recognize it.

It is not supposed to do so. It searches for a file templates/masterhead.php, sincetemplates/masterhead-templatename.php was not found.

The function specification get_template_part( 'templates/masthead', $my_theme->Template); searches like this:


Which file is found, that is included and the searching obviously stops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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