所有类型的模板都有filters 以控制加载的模板。对于页面,有page_template
, 您可以查看的位置the queried page 或查看ID是否存在于an array of a page\'s ancestor IDs.
function wpd_page_template_filter( $template ){
// the ID of Mango page
$parent_page = 20;
if( $parent_page == get_queried_object_id()
|| in_array( $parent_page, get_ancestors( get_queried_object_id(), \'page\' ) ) ){
$template = locate_template( \'mango.php\', false );
}
return $template;
}
add_filter( \'page_template\', \'wpd_page_template_filter\' );