您可以使用template_include
筛选以从主题加载模板文件:
add_filter( \'template_include\', \'my_page_template\', 99 );
function my_page_template( $template ) {
if ( isset( $_GET[\'info\'] ) && $_GET[\'info\'] == \'some_value\' ) {
$new_template = locate_template( array(\'my-template.php\' ) );
if ( \'\' != $new_template ) {
return $new_template ;
}
}
return $template;
}
如果要在任何其他位置检查tempalte文件,可以。例如,您可以检查主题/子主题是否有要加载的模板文件,如果没有,请从插件路径加载默认模板文件:
add_filter( \'template_include\', \'my_page_template\', 99 );
function my_page_template( $template ) {
if ( isset( $_GET[\'info\'] ) && $_GET[\'info\'] == \'some_value\' ) {
$new_template = locate_template( array(\'my-template.php\' ) );
if ( \'\' != $new_template ) {
//my-template.php file has been found in theme/child theme folder
$template = $new_template ;
} else {
//load template callback when the file dosen\'t exist in theme
$template = PATH_TO_MY_PLUGIN . \'/my-template.php\';
}
}
return $template;
}
另请参见
template_redirect 滤器