我正在为worpdress使用自定义帖子类型UI插件,并创建了自定义"页面"。我已将这些页面设置为能够使用页面模板下拉列表,但想知道是否有人知道为不同的帖子类型显示单独的页面模板的方法?
答案 0 :(得分:0)
您需要使用theme_page_templates
过滤器钩子来删除您不希望为每种帖子类型看到的模板。加载模板后,包括在编辑屏幕中显示,返回的模板首先传递给此函数:
apply_filters ( 'theme_page_templates', array $page_templates, WP_Theme $this, WP_Post|null $post )
要实现,您将使用类似以下代码的内容:
function filter_theme_page_templates( $templates, $theme, $post ){
// make sure we have a post so we know what to filter
if ( !empty( $post ) ){
// get the post type for the current post
$post_type = get_post_type( $post->ID );
// switch on the post type
switch( $post_type ){
case 'custom-post-type':
// remove anything we don't want shown for the custom post type
// array is keyed on the template filename
unset( $templates['page-template-filename.php'] );
break;
default:
// if there is no match it will return everything
break;
}
}
// return the (maybe) filtered array of templates
return $templates;
}
add_filter( 'theme_page_templates', 'filter_theme_page_templates', 10, 3 );