有许多模板过滤器可用于更改模板层次结构。看看template hierarchy page 在提供的过滤器和示例中。
下面是示例的修改版本,使用single_template
. 它检查特定的自定义帖子类型并加载主题的page.php
模板(如果存在):
function wpa54721_single_template( $templates = \'\' ){
// get data for the current post and check type
$this_post = get_queried_object();
if( \'my_custom_post_type\' == $this_post->post_type ):
// $templates could be empty, a string, or an array,
// we need to check and alter it appropriately
if( !is_array( $templates ) && !empty( $templates ) ):
// it\'s a string
$templates = locate_template( array( \'page.php\', $templates ), false );
elseif( empty( $templates ) ):
// it\'s empty
$templates = locate_template( \'page.php\', false );
else:
// it\'s an array
$new_template = locate_template( array( \'page.php\' ) );
if( !empty( $new_template ) ) array_unshift( $templates, $new_template );
endif;
endif;
return $templates;
}
add_filter( \'single_template\', \'wpa54721_single_template\' );