Load template inside a parent template

if ( condition ) 
{
    get_template_part( 'path/to/template', 'mobile' );
}
else
{
    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;
            break;

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

// Now attach the above function to get_template_part()
function wpse21352_template_actions()
{
    $wpse21352_mobile_files = array(
         'header'
        ,'logo'
        ,'content'
        // ,'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.

Leave a Comment