在我当前的主题目录中,我创建了一个名为“mobile”的文件夹,其中包含一组单独的模板文件,所有这些文件都针对mobile进行了优化。
现在,假设我设置了一个检测移动设备的函数,我希望wordpress现在加载“mytheme/mobile/header.php”,而不是主题根标题。php和索引文件。
我是否可以告诉wordpress在模板子文件夹中查找模板文件?
非常感谢
在我当前的主题目录中,我创建了一个名为“mobile”的文件夹,其中包含一组单独的模板文件,所有这些文件都针对mobile进行了优化。
现在,假设我设置了一个检测移动设备的函数,我希望wordpress现在加载“mytheme/mobile/header.php”,而不是主题根标题。php和索引文件。
我是否可以告诉wordpress在模板子文件夹中查找模板文件?
非常感谢
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()
对于子目录可能是更好的选择。请阅读评论。Wordpress已安装到我的根目录(/根/)。我有一个子域名(dammani.techbrij.com),我想从子域名访问主域名的一些文件。我做了以下事情:include_once(\'/root/wp-config.php\'); include_once(\'/root/wp-load.php\'); include_once(\'/root/wp-includes/wp-db.php\'); 现在,“http://techbrij.com/dammani“工作正常,但是”