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

Inside of my current theme directory, I created a folder called "mobile" which contains a separate set of template files, all optimized for mobile.

Now, say I setup a function that detects a mobile device, I would like wordpress to now load "mytheme/mobile/header.php" Instead of the themes root header.php and index files.

Is there some way I can tell wordpress to look in a template subfolder for the template files?

Many thanks

share|improve this question
up vote 1 down vote accepted
if ( condition ) 
    get_template_part( 'path/to/template', 'mobile' );
    get_template_part( 'template', 'mobile' ); // in rootpath

You can also intercept the behavior for get_template_part() with an action hook:

// Source in get_template_part
do_action( "get_template_part_{$slug}", $slug, $name );

function wpse21352_template_part_cb( $slug, $name )
    switch ( $name ) 
        case 'mobile' :
            $slug = 'mobile/'.$slug;

        case 'tablet' :
            $slug = 'tablet/'.$slug;
    return $slug;

// Now attach the above function to get_template_part()
function wpse21352_template_actions()
    $wpse21352_mobile_files = array(
        // ,'etc...'

    foreach ( $wpse21352_mobile_files as $slug )
        add_action( 'wpse21352_template_part_cb', 'get_template_part_'.$slug, 10, 2 );
add_action( 'after_setup_theme', 'wpse21352_template_actions' ); // maybe some other hook

EDIT: As to @Otto comment, locate_template() might be a better choice for subdirectories. Please read the comments.

share|improve this answer
You should use locate_template() here, rather than get_template_part(); the former is intended to handle subdirectories, but the latter really isn't. – Chip Bennett Jun 28 '11 at 15:53
@Chip can you show me a quick example on how I can load all template files from a subfolder? I understand the concept but I cant seem to apply it right. Thanks – Levi Jun 28 '11 at 16:04
@Chip Bennet It works the way I wrote, but using the more native fn will work too. – kaiser Jun 28 '11 at 16:16
@kaiser let me play around with an idea, and then I'll add something to your answer. I want to play around with taking advantage of passing an array to locate_template(). – Chip Bennett Jun 28 '11 at 19:11
Basically, locate_template is designed to take a relative path as the first argument. Therefore it explicitly works with paths. get_template_part, on the other hand, is meant to take two "words" or "slugs" if you prefer, and to build the template path from them. So the fact that it works with subdirectories is more of a side effect instead of intended design. Future adjustments might break it if you try using subdirectories with it, but that won't happen with locate_template. Please use locate_template instead. – Otto Jun 28 '11 at 19:35

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.