首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示自定义帖子类型的不同页面模板

显示自定义帖子类型的不同页面模板
EN

Stack Overflow用户
提问于 2015-08-23 09:20:32
回答 1查看 583关注 0票数 0

我正在使用的自定义帖子类型的用户界面插件,为文字着装,并已创建自定义“页面”。我已经将这些页面设置为能够使用页面模板下拉菜单,但我想知道是否有人知道如何为不同的帖子类型显示不同的页面模板?

EN

回答 1

Stack Overflow用户

发布于 2015-08-23 11:15:57

您将需要theme_page_templates过滤器挂钩来删除您不希望在每个帖子类型中看到的模板。加载模板时,包括在编辑屏幕中显示时,返回的模板首先传递给此函数:

代码语言:javascript
复制
apply_filters ( 'theme_page_templates', array $page_templates, WP_Theme $this, WP_Post|null $post )

为了实现,您将使用类似于以下代码的代码:

代码语言:javascript
复制
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 );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32162314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档