Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Thanks!

share|improve this question

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:

STYLESHEETPATH/templates/masthead-themename.php
STYLESHEETPATH/templates/masthead.php
TEMPLATEPATH/templates/masthead-themename.php
TEMPLATEPATH/templates/masthead.php

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

share|improve this answer

Your Answer

 
discard

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.