分层自定义帖子类型的模板

时间:2015-09-25 作者:Steve Taylor

我对定义为层次结构的自定义帖子类型的模板有点困惑。我将CPT设置为hierarchy,这并不是为了有嵌套的层次结构,而是为了让它们可以通过简单的页面排序插件排序。代码如下:

register_post_type(
    \'case_study\', array(
        \'label\'                 => _x( \'case studies\', \'post type plural name\' ),
        \'labels\'                => array(
            \'name\'                  => _x( \'Case studies\', \'post type general name\' ),
            \'singular_name\'         => _x( \'Case study\', \'post type singular name\' ),
            \'menu_name\'             => _x( \'Case studies\', \'admin menu\' ),
            \'name_admin_bar\'        => _x( \'Case study\', \'add new on admin bar\' ),
            \'add_new\'               => _x( \'Add New\', \'case study\' ),
            \'add_new_item\'          => __( \'Add New Case study\' ),
            \'new_item\'              => __( \'New Case study\' ),
            \'edit_item\'             => __( \'Edit Case study\' ),
            \'view_item\'             => __( \'View Case study\' ),
            \'all_items\'             => __( \'All Case studies\' ),
            \'search_items\'          => __( \'Search Case studies\' ),
            \'parent_item_colon\'     => __( \'Parent Case studies:\' ),
            \'not_found\'             => __( \'No Case studies found.\' ),
            \'not_found_in_trash\'    => __( \'No Case studies found in Trash.\' )
        ),
        \'public\'                => true,
        \'publicly_queryable\'    => true,
        \'show_ui\'               => true,
        \'show_in_nav_menus\'     => true,
        \'show_in_menu\'          => true,
        \'show_in_admin_bar\'     => true,
        \'menu_position\'         => 20, // Below Pages
        \'menu_icon\'             => \'dashicons-clipboard\', // @link https://developer.wordpress.org/resource/dashicons/
        \'query_var\'             => false,
        \'rewrite\'               => array( \'slug\' => \'case-study\', \'with_front\' => false ),
        \'capability_type\'       => \'case_study\',
        \'map_meta_cap\'          => false,
        \'capabilities\' => array(
            \'publish_posts\'         => \'publish_case_studies\',
            \'edit_posts\'            => \'edit_case_studies\',
            \'edit_others_posts\'     => \'edit_others_case_studies\',
            \'delete_posts\'          => \'delete_case_studies\',
            \'delete_others_posts\'   => \'delete_others_case_studies\',
            \'read_private_posts\'    => \'read_private_case_studies\',
            \'edit_post\'             => \'edit_case_study\',
            \'delete_post\'           => \'delete_case_study\',
            \'read_post\'             => \'read_case_study\',
        ),
        \'has_archive\'           => false,
        \'hierarchical\'          => true, // Set to true to allow ordering
        \'supports\'              => array( \'title\', \'editor\', \'custom-fields\', \'thumbnail\', \'revisions\', \'pilau-author\' ),
        \'taxonomies\'            => array( \'topic\' ),
    )
);
默认情况下,分层CPT使用该页面。php模板,而不是单一模板。php——我花了一段时间才意识到这一点,但这是有道理的。

然而,现在我想专门为这个CPT和single-case\\u研究创建一个模板。php不工作。page-case\\u也没有研究。php。

如何专门为分层CPT创建模板?

2 个回复
SO网友:Steve Taylor

我感觉到我仍然缺少一些东西,但现在我已经用另一种方式来处理了。因为CPT拥有hierarchical 设置为true 是这样的Simple Page Ordering 将起作用(我实际上并不追求嵌套的层次结构),我回到插件的常见问题,发现可以排序非层次结构的帖子类型。要么包括\'page-attributes\'supports 数组或使用simple_page_ordering_is_sortable 筛选,然后您可以单击帖子管理列表页面上的“按顺序排序”并拖放。

SO网友:amapparat

老问题,但可能对其他人有帮助:

您可以在page中执行类似操作。php:

$postType = get_post_type();

if($postType == \'case_study\'){
   include(\'single-case_study.php\');
} else{
   // Do your page.php stuff

相关推荐

Templates for Mobile Site

是否有任何内置方法可以根据浏览器大小显示不同的模板(即移动设备检测)?我做了一些研究,我能找到的只是大量插件,它们的功能远远超出了我的需要。我基本上只需要一种方法,将移动目录添加到我的主题中,并为移动用户显示该主题。