Categories under custom post types doesn’t show properly

Categories and Taxonomies are not exactly the same.

A category is a default taxonomy

You can use categories with custom post types however its better to use custom taxonomies because the truth is, they are an extremely powerful way to group various items in all sorts of ways.

add_action( 'init', 'wpsites_custom_taxonomy_types' );
function wpsites_custom_taxonomy_types() {

register_taxonomy( 'cpt-type', 'cpt',
    array(
        'labels' => array(
            'name'          => _x( 'Types', 'taxonomy general name', 'themename' ),
            'add_new_item'  => __( 'Add New CPT Type', 'themename' ),
            'new_item_name' => __( 'New CPT Type', 'themename' ),
        ),
        'exclude_from_search' => true,
        'has_archive'         => true,
        'hierarchical'        => true,
        'rewrite'             => array( 'slug' => 'cpt-type', 'with_front' => false ),
        'show_ui'             => true,
        'show_tagcloud'       => false,
    )
);

}

If you’ve added code like above which adds the option to create custom taxonomy types, then you’ll need to create a file named something like:

taxonomy-cpt-type.php

Where cpt is the name of your custom post type.

You can use the code above in your child themes functions file and replace all instances of cpt and CPT with the name of your custom post type.

You would also need to add this line to the code which registers your custom post type:

'taxonomies'   => array( 'cpt-type' ),

Here’s a working example:

add_action( 'init', 'wpsites_custom_post_type' );
function wpsites_custom_post_type() {

register_post_type( 'cpt',
    array(
        'labels' => array(
            'name'          => __( 'CPT', 'wpsites' ),
            'singular_name' => __( 'CPT', 'wpsites' ),
        ),
        'has_archive'  => true,
        'hierarchical' => true,
        'menu_icon'    => 'dashicons-portfolio',
        'public'       => true,
        'rewrite'      => array( 'slug' => 'cpt', 'with_front' => false ),
        'supports'     => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ),
        'taxonomies'   => array( 'cpt-type' ),

    )
);

}

Again, replace all instances of cpt and CPT with the name of your custom post type.

For custom post type archive pages, use something like this:

archive-cpt.php

For custom post type single pages, use something like this:

single-cpt.php

Again, replace cpt in the file name with the name of your custom post type.

Leave a Comment