Sometimes, you might want to use the categories that are native to posts on other custom post types. Here is how you do that.
When you register the custom post type, add this line to your code. The term ‘category’ is what will connect your custom post type to use the categories on posts.
‘taxonomies’ => array( ‘category’ ),
So, your custom post type will look like this:
function create_posttype() {
register_post_type( ‘article’,
array(
‘labels’ => array(
‘name’ => _x( ‘Articles’, ‘Post Type General Name’, ‘theme’ ),
‘singular_name’ => _x( ‘Article’, ‘Post Type Singular Name’, ‘theme’ ),
‘menu_name’ => __( ‘Articles’, ‘theme’ ),
‘parent_item_colon’ => __( ‘Parent Article’, ‘theme’ ),
‘all_items’ => __( ‘All Articles’, ‘theme’ ),
‘view_item’ => __( ‘View Article’, ‘theme’ ),
‘add_new_item’ => __( ‘Add New Article’, ‘theme’ ),
‘add_new’ => __( ‘Add New’, ‘theme’ ),
‘edit_item’ => __( ‘Edit Article’, ‘theme’ ),
‘update_item’ => __( ‘Update Article’, ‘theme’ ),
‘search_items’ => __( ‘Search Articles’, ‘theme’ ),
‘not_found’ => __( ‘Not Found’, ‘theme’ ),
‘not_found_in_trash’ => __( ‘Not found in Trash’, ‘theme’ ),
),
‘show_in_rest’ => true,
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘page-attributes’, ‘thumbnail’ ),
‘taxonomies’ => array( ‘category’ ),
‘public’ => true,
‘has_archive’ => false,
‘hierarchical’ => false,
‘menu_icon’ => ‘dashicons-text-page’,
‘rewrite’ => array(‘slug’ => ‘article’, ‘with_front’ => FALSE),
)
);
// Hooking up our function to theme setup
add_action( ‘init’, ‘create_posttype()’ );
Leave a Reply