Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created a Custom Post Type, and here's the entire code:

// Register Custom Post Type
function wrestler_register() {

    $labels = array(
        'name'                => _x( 'Wrestlers', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Wrestler', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Wrestler Profiles', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
        'all_items'           => __( 'All Items', 'text_domain' ),
        'view_item'           => __( 'View Item', 'text_domain' ),
        'add_new_item'        => __( 'Add New Profile', 'text_domain' ),
        'add_new'             => __( 'Add New', 'text_domain' ),
        'edit_item'           => __( 'Edit Item', 'text_domain' ),
        'update_item'         => __( 'Update Item', 'text_domain' ),
        'search_items'        => __( 'Search Item', 'text_domain' ),
        'not_found'           => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
    );
    $rewrite = array(
        'slug'                => 'wrestler',
        'with_front'          => true,
        'pages'               => false,
        'feeds'               => false,
    );
    $args = array(
        'label'               => __( 'wrestler_profiles', 'text_domain' ),
        'description'         => __( 'Profiles of WWE Superstars & Divas', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'thumbnail', 'custom-fields', ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => url + '/icon.png',
        'can_export'          => true,
        'has_archive'         => false,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'rewrite'             => $rewrite,
        'query_var'           => 'wrestler',
        'capability_type'     => 'page',
    );
    register_post_type( 'wrestler_profiles', $args );

}

// Hook into the 'init' action
add_action( 'init', 'wrestler_register', 0 );

Now, I have also added single-wrestler.php to my theme's root, but every time I'm viewing the page it lands on "Single.php".

Thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

For some reason wordpress don't honor Template_Hierarchy. The best way to get around this problem is to "force" wordpress to use the custom single.php template that you created. To accomplish that, add the following function underneath your CPT.

   /* Information Posts Template selection - a single.php just for our wrestler_profiles */
function pietergoosen_info_template_include( $original_template ) {
    if ( isset( $wp->query_vars['wrestler_profiles'] ) && false == $wp->query_vars['wrestler_profiles']  ) {
        return get_template_directory() . '/single-wrestler.php';
    } else {
        return $original_template;
    }
}

add_filter( 'template_include', 'pietergoosen_info_template_include' );

Just remember, if you are using your CPT in a child theme or in a plugin, change get_template_directory() accordingly

share|improve this answer
    
Thanks, it works! –  Hardeep Asrani Mar 16 at 9:52
    
Glad I could help. –  Pieter Goosen Mar 16 at 9:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.