【发布时间】:2015-08-23 01:20:32
【问题描述】:
我正在为 wrpdress 使用自定义帖子类型 UI 插件,并创建了自定义“页面”。我已将这些页面设置为能够使用页面模板下拉列表,但想知道是否有人知道为不同帖子类型显示单独页面模板的方法?
【问题讨论】:
标签: wordpress custom-post-type
我正在为 wrpdress 使用自定义帖子类型 UI 插件,并创建了自定义“页面”。我已将这些页面设置为能够使用页面模板下拉列表,但想知道是否有人知道为不同帖子类型显示单独页面模板的方法?
【问题讨论】:
标签: wordpress custom-post-type
您将需要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 );
【讨论】:
$templates 数组复制到一个新数组中并返回。