Switching between custom templates in a post type of the admin menu

I created a post type in the admin menu through

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' );

and I realised I can’t use the function “Templates” in the Page attributes.
It is only available in category “pages”, but not in my created post typ.
This one is missing:

Page Attributes

Does anyone know how I can make it available?
Thanks a lot.

1 Answer
1

When at least one template exists for a post type, the ‘Post Attributes’ meta box will be displayed in the back end, without the need to add post type support for 'page-attributes'. The title ‘Post Attributes’ can be customized per post type using the 'attributes' label when registering a post type.

Templates for custom post types have been introduced in WordPress 4.7 and you can define one like this:

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

// … your code here

I recommend you to check my official post about it to learn more.

Leave a Comment