I am trying to make a custom post type for short texts, notici
. I want to register a custom taxonomy for Notice categories. I have tried the code below in my plugin. The tags does show up and works, however I cannot see or choose categories when editing the notici post.
<?php
class Notici_Register_Cpt {
function __construct( $plugin_name, $version ) {
// Register the custom post type
add_action( 'init', array( $this, 'notici_register_cpt' ) );
// Add notici categories
add_action( 'init', array( $this, 'notici_register_category_taxonomy' ), 0 );
}
function notici_register_cpt() {
$labels = array(
'name' => _x( 'Notices', 'post type general name' ),
// omitted for brevity
);
$args = array(
'label' => __( 'Notices' ),
'labels' => $labels,
'public' => true,
'show_in_rest' => true,
'has_archive' => true,
'can_export' => true,
'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'menu_icon' => 'dashicons-exerpt-view',
'hierarchical' => false,
'rewrite' => array( 'slug' => get_option( 'notici_slug' ) ),
'supports' => array( 'title', 'thumbnail', 'excerpt', 'editor' ),
'show_in_nav_menus' => true,
'taxonomies' => array( 'noticicategory', 'post_tag' ),
);
register_post_type( 'notici', $args );
flush_rewrite_rules();
}
// Register Custom Taxonomy
function notici_register_category_taxonomy() {
$labels = array(
'name' => _x( 'Notice categories', 'Taxonomy General Name', 'notici' ),
// omitted for brevity
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array( 'slug' => 'notice-category' ),
);
register_taxonomy( 'noticicategory', array( 'notici' ), $args );
}
}
Some code skipped for brevity, here is everything.
I am trying to make a custom post type for short texts, notici
. I want to register a custom taxonomy for Notice categories. I have tried the code below in my plugin. The tags does show up and works, however I cannot see or choose categories when editing the notici post.
<?php
class Notici_Register_Cpt {
function __construct( $plugin_name, $version ) {
// Register the custom post type
add_action( 'init', array( $this, 'notici_register_cpt' ) );
// Add notici categories
add_action( 'init', array( $this, 'notici_register_category_taxonomy' ), 0 );
}
function notici_register_cpt() {
$labels = array(
'name' => _x( 'Notices', 'post type general name' ),
// omitted for brevity
);
$args = array(
'label' => __( 'Notices' ),
'labels' => $labels,
'public' => true,
'show_in_rest' => true,
'has_archive' => true,
'can_export' => true,
'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'menu_icon' => 'dashicons-exerpt-view',
'hierarchical' => false,
'rewrite' => array( 'slug' => get_option( 'notici_slug' ) ),
'supports' => array( 'title', 'thumbnail', 'excerpt', 'editor' ),
'show_in_nav_menus' => true,
'taxonomies' => array( 'noticicategory', 'post_tag' ),
);
register_post_type( 'notici', $args );
flush_rewrite_rules();
}
// Register Custom Taxonomy
function notici_register_category_taxonomy() {
$labels = array(
'name' => _x( 'Notice categories', 'Taxonomy General Name', 'notici' ),
// omitted for brevity
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array( 'slug' => 'notice-category' ),
);
register_taxonomy( 'noticicategory', array( 'notici' ), $args );
}
}
Some code skipped for brevity, here is everything.
You have to make your taxonomy visible in rest - otherwise it won’t be visible in Block Editor (Gutenberg).
There is show_in_rest
arg for register_taxonomy
. It’s default value is false, so you have to set it to true.