admin管理员组

文章数量:1122832

Edit: Now I've found that this is Hybrid Core, and apparently the template hierarchy is different. I still haven't solved my problem below though.

In addition to the title. My thinking is that if I am able to select a template in the backend, it should honor that selection. It doesn't change though. I've tested the other template file in place of the default, and it works.

This is the function to add my custom post type. This works just fine.

add_action( 'init', 'register_cpt_location' );

function register_cpt_location() {

$labels = array(
    'name' => _x( 'Locations', 'info_location' ),
    'singular_name' => _x( 'Location', 'info_location' )
);

$args = array(
    'labels' => $labels,
    'description' => 'Custom Post Type for Location',
    'hierarchical' => true,
    'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'revisions', 'page-attributes' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 6,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => true,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => array( 
        'slug' => 'location', 
        'with_front' => false,
        'feeds' => true,
        'pages' => true
    ),
    'menu_icon' => 'dashicons-location',
    'capability_type' => 'post'
);

register_post_type( 'info_location', $args );   
}

When I add a template for the custom post type (only to use on a few posts), the metabox shows up with my new template. However, the live page still uses the default template for the custom post type.

The default template is "info_location.php".

This is my head info for the template file "info_location-fm.php".

/*
* Template Name: Location-fm
* Template Post Type: info_location
*/

showing the drop down for templates (below)

new template selected and saved, still doesn't work (below)

Edit: Now I've found that this is Hybrid Core, and apparently the template hierarchy is different. I still haven't solved my problem below though.

In addition to the title. My thinking is that if I am able to select a template in the backend, it should honor that selection. It doesn't change though. I've tested the other template file in place of the default, and it works.

This is the function to add my custom post type. This works just fine.

add_action( 'init', 'register_cpt_location' );

function register_cpt_location() {

$labels = array(
    'name' => _x( 'Locations', 'info_location' ),
    'singular_name' => _x( 'Location', 'info_location' )
);

$args = array(
    'labels' => $labels,
    'description' => 'Custom Post Type for Location',
    'hierarchical' => true,
    'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'revisions', 'page-attributes' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 6,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => true,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => array( 
        'slug' => 'location', 
        'with_front' => false,
        'feeds' => true,
        'pages' => true
    ),
    'menu_icon' => 'dashicons-location',
    'capability_type' => 'post'
);

register_post_type( 'info_location', $args );   
}

When I add a template for the custom post type (only to use on a few posts), the metabox shows up with my new template. However, the live page still uses the default template for the custom post type.

The default template is "info_location.php".

This is my head info for the template file "info_location-fm.php".

/*
* Template Name: Location-fm
* Template Post Type: info_location
*/

showing the drop down for templates (below)

new template selected and saved, still doesn't work (below)

Share Improve this question edited Aug 11, 2017 at 21:09 Will Braden asked Aug 11, 2017 at 14:18 Will BradenWill Braden 13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

in your code:

$labels = array(
    'name' => _x( 'Locations', 'info_locaiton' ),
    'singular_name' => _x( 'Location', 'info_location' )
);

use this:

$labels = array(
    'name' => _x( 'Locations', 'info_location' ),
    'singular_name' => _x( 'Location', 'info_location' )
);

In $labels variable name array value has different slug. I think this can be a problem. So, you can correct it and check. Or you can use custom post type plugin to make custom post type easily.

Hope this will help!

本文标签: