Mr.Singh
Mr.Singh

Reputation: 1791

WordPress Custom Post Type Choose Template Dropdown Missing

I am new at WordPress, I created a Custom Post Type, but even after adding "page-attributes" I do not see the template dropdown for choosing my custom template only "Order" field is visible.

My theme has 3 different menus(top, main and footer) where all these menus have same templates for each menu, for example: the main menu has central, east, west, north and south location, have the same layout, same for Top menu as well.

Each location in the main menu and Top menu is an archive page, where I can show the listing of that location posts for further viewing. But by following the conventional archive-{post_type}.php I have to create a new archive page for each location.

Here is the code for main menu:

[
    "capability_type" => "post",
    "description" => "Holds our location's specific data",
    "public" => true,
    "menu_position" => 5,
    "has_archive" => true,
    "show_admin_column" => true,
    "supports" => [
        "title",
        "editor",
        "thumbnail",
        "excerpt",
        "revisions",
        "page-attributes"
    ],
    "taxonomies" => [
        "post_tag"
    ],
    "labels" => [
        "name" => "Locations",
        "singular_name" => "Location",
        "add_new" => "Add Location",
        "add_new_item" => "Add Location" ,
        "edit_item" => "Edit Location",
        "new_item" => "New Location",
        "all_items" => "Locations" ,
        "view_item" => "View Location",
        "search_items" => "Search Location",
        "not_found" => "No Locations found",
        "not_found_in_trash" => "No Locations found in the Trash",
        "parent_item_colon" => "",
        "menu_name" => "Locations"
    ]
]

Here is the output:

enter image description here

Please help in fixing this issue.

Thanks

Upvotes: 2

Views: 6890

Answers (3)

Randy Kilwag
Randy Kilwag

Reputation: 11

Suppose you can't or don't want to modify theme templates because your changes will disappear when the theme is updated. You can add an action to do this.

function my_cpt_templates( $templates ) {
   $my_templates = array(
     'template_id_1' => 'Template 1',
     'template_id_2' => 'Template 2',
   );
   return $templates;
}

add_filter( 'theme_custom_post_type_templates', 'my_cpt_templates' );

In the filter call above, replace 'custom_post_type' with the name of your custom post type. You can grab the id's of the existing template choices by inspecting the dropdown menu template chooser in a page where you can see it.

You'll also have to set the following when declaring the post type if you want it to show up with Gutenberg.

'show_in_rest' => true, 

This helpful source is where I learned most of this.

Upvotes: 0

Declan Kay
Declan Kay

Reputation: 136

Make sure on the template file you have created, that you have the following code at the very top:

/**
* Template Name: Test Template
*/

If this does not fix the issue, make sure than inside your theme folder, you have an index.php and style.css files.

Upvotes: 0

ngearing
ngearing

Reputation: 1365

Previously this was only available to pages. But as of 4.7 it's available for all post types, you just need to add Template Post Type to the file header.

eg.

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

// … your code here

https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/

Upvotes: 11

Related Questions