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

1 Answer 1

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.

  • You should use locate_template() here, rather than get_template_part(); the former is intended to handle subdirectories, but the latter really isn't. Commented Jun 28, 2011 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
    Commented Jun 28, 2011 at 16:04
  • @Chip Bennet It works the way I wrote, but using the more native fn will work too.
    – kaiser
    Commented Jun 28, 2011 at 16:16
  • 2
    @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(). Commented Jun 28, 2011 at 19:11
  • 1
    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
    Commented Jun 28, 2011 at 19:35

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.