template_include介绍
这个过滤器钩子在WordPress包含预定的模板文件之前立即执行。这可以用来覆盖WordPress的默认模板行为。
template_include例子
如果发现新的模板文件,此示例在名为“投资组合”的页面上包含一个新模板。
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
if ( is_page( 'portfolio' ) ) {
$new_template = locate_template( array( 'portfolio-page-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
源文件
应用此过滤器 wp-includes/template-loader.php
相关函数
Action Hook:template_redirect
Filter Hook:template_include
Filter Hook:single_template
Filter Hook:{$ type} _template