我正在使用的自定义帖子类型的用户界面插件,为文字着装,并已创建自定义“页面”。我已经将这些页面设置为能够使用页面模板下拉菜单,但我想知道是否有人知道如何为不同的帖子类型显示不同的页面模板?
发布于 2015-08-23 11:15:57
您将需要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 );https://stackoverflow.com/questions/32162314
复制相似问题