How can I add single catogory for custom post type?

You can add this line to the code which registers your Custom Post Type:

'taxonomies'   => array( 'category' ),

Or you could add Taxonomies rather than categories:

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

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

}

Then add this line to the code which registers your Custom Post Type.

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

Here’s all the code which registers a Custom Post Type:

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

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

    )
);

}

Leave a Comment