WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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