if ( condition )
{
get_template_part( \'path/to/template\', \'mobile\' );
}
else
{
get_template_part( \'template\', \'mobile\' ); // in rootpath
}
您还可以拦截的行为
get_template_part()
带动作钩:
// 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: 至于@Otto评论,
locate_template()
对于子目录可能是更好的选择。请阅读评论。