在管理菜单的帖子类型中切换自定义模板

时间:2018-02-03 作者:Martin

我通过在“管理”菜单中创建了一个帖子类型

function codex_custom_init() {

$labels = array(
    \'name\'               => _x( \'Book\', \'post type general name\', \'your-plugin-textdomain\' ),
    \'singular_name\'      => _x( \'Book\', \'post type singular name\', \'your-plugin-textdomain\' ),
    \'menu_name\'          => _x( \'Books\', \'admin menu\', \'your-plugin-textdomain\' ),
    \'name_admin_bar\'     => _x( \'Books\', \'add new on admin bar\', \'your-plugin-textdomain\' ),
    \'add_new\'            => _x( \'add Books\', \'book\', \'your-plugin-textdomain\' ),
    \'add_new_item\'       => __( \'add Books\', \'your-plugin-textdomain\' ),
    \'new_item\'           => __( \'add Books\', \'your-plugin-textdomain\' ),
    \'edit_item\'          => __( \'add Books\', \'your-plugin-textdomain\' ),
    \'view_item\'          => __( \'add Books\', \'your-plugin-textdomain\' ),
    \'all_items\'          => __( \'add Books\', \'your-plugin-textdomain\' ),
    \'search_items\'       => __( \'Seite suchen\', \'your-plugin-textdomain\' ),
    \'parent_item_colon\'  => __( \'Parent Seiten:\', \'your-plugin-textdomain\' ),
    \'not_found\'          => __( \'No Books.\', \'your-plugin-textdomain\' ),
    \'not_found_in_trash\' => __( \'No Books.\', \'your-plugin-textdomain\' ),
    \'archives\' => __( \'Books Archiv\' ),
    \'attributes\' => __( \'Books Attribute\' ),
    \'insert_into_item\' => __( \'add Books\' ),
    \'uploaded_to_this_item\' => __( \'upload Books\' ),
    \'page\' => get_stylesheet_directory_uri().\'/template-test.php\',
    \'capabilities\' => array(
    \'edit_post\'          => \'edit_Books\',
    \'read_post\'          => \'read_Books\',
    \'delete_post\'        => \'delete_Books\',
    \'delete_posts\'       => \'delete_Books\',
    \'edit_posts\'         => \'edit_Books\',
    \'edit_others_posts\'  => \'edit_others_Books\',
    \'publish_posts\'      => \'publish_Books\',
    \'read_private_posts\' => \'read_private_Books\',
    \'create_posts\'       => \'create_Books\',),);

     $args = array(
    \'labels\'             => $labels,
    \'description\'        => __( \'Description.\', \'your-plugin-textdomain\' ),
    \'public\'             => false,
    \'publicly_queryable\' => false,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'maths\' ),
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => 20,
    \'menu_icon\'          => \'dashicons-welcome-learn-more\',
        \'hierarchical\'       => true,
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\',\'revisions\', \'page-attributes\', \'post-formats\'),
        \'post_type\' => \'events\',
    \'posts_per_page\' => 10,
    );

register_post_type( \'Books\', $args );}}
add_action( \'init\', \'codex_custom_init\' );
我意识到我不能在页面属性中使用“模板”功能。它仅在“页面”类别中可用,但在我创建的帖子类型中不可用。缺少此项:

Page Attributes

有人知道我如何提供吗?非常感谢。

1 个回复
最合适的回答,由SO网友:swissspidy 整理而成

当一个帖子类型至少存在一个模板时,“帖子属性”元框将显示在后端,无需为添加帖子类型支持\'page-attributes\'. 标题“帖子属性”可以根据帖子类型使用\'attributes\' 注册帖子类型时添加标签。

WordPress 4.7中引入了自定义帖子类型的模板,您可以这样定义一个模板:

<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, product
*/

// … your code here
我建议你检查一下my official post about it 了解更多信息。

结束

相关推荐

Wp-config.php中的if语句,这可能吗?

我已经到处搜索了,但找不到任何确定的东西。我有WordPress multisite,我有一个插件(在每个站点上激活),需要在wp config中设置slug参数。php。不幸的是,网络的每个站点都需要不同的slug参数。我想知道是否可以在wp config中放入if语句。php,以便根据$_SERVER[\'HTTP_HOST\'] 变量代码如下:if ( stristr( $_SERVER[\'HTTP_HOST\'], \'site.com\' ) ) { define(\