How to add page attribute “Template” to custom post

Please tell me, is it possible?admin bar
I created a custom post, but nowhere can I find information how to add the ability to select a template of page:

function trav_register_region_post_type() {
    $labels = array(
        'name'                => _x( 'Regions', 'Post Type General Name', 'trav' ),
        'singular_name'       => _x( 'Region', 'Post Type Singular Name', 'trav' ),
        'menu_name'           => __( 'Regions', 'trav' ),
        'all_items'           => __( 'All Regions', 'trav' ),
        'view_item'           => __( 'View Region', 'trav' ),
        'add_new_item'        => __( 'Add New Region', 'trav' ),
        'add_new'             => __( 'New Region', 'trav' ),
        'edit_item'           => __( 'Edit Regions', 'trav' ),
        'update_item'         => __( 'Update Regions', 'trav' ),
        'search_items'        => __( 'Search Regions', 'trav' ),
        'not_found'           => __( 'No Regions found', 'trav' ),
        'not_found_in_trash'  => __( 'No Regions found in Trash', 'trav' ),
    );
    $args = array(
        'label'               => __( 'region', 'trav' ),
        'labels'              => $labels,
        'public'              => true,
        'hierarchical'        => true,
        'has_archive'         => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'publicly_queryable'  => true,
        'supports'            => array( 'title', 'editor', 'thumbnail', 'page-attributes'),
        'exclude_from_search' => false,
        'capability_type'     => 'page'
    );
    register_post_type( 'region', $args );
}

1 Answer
1

Since WordPress version 4.7 Post-Type-Templates are enabled in the WordPress core.

You dont need to specify the templates in the register_post_type function.
Instead, just create your post-templates like you would do with your normal page-templates.

So for example, duplicate the single.php file, rename it and add the following code to the top of the file:

<?php
/*
Template Name: My Post Template Name 
Template Post Type: post, region 
*/

After that, you should see a Post Attributes box in the backend, in this case on the post-types “post” and “region”, where you can select your template.

You can find some more info here https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/

You can also read about normal (old) page-templates in WordPress here.

I hope this is what you are looking for.

Leave a Comment