Right now, you have no taxonomy rewrite. The taxonomy will try to use the taxonomy slug by default but will run into a conflict with your post type slug since they are both gifts
. In my example below, I’ve changed your slugs to better reflect their origin, then I use the rewrite
parameter and has_archive
parameter to define what the permalinks should be.
// Custom post types GIFTS
function gifts_post_type(){
$args = array(
'labels' => array(
'name' => 'Gifts',
'singular_name' => 'Gift',
),
'description' => __( 'Gifts' ),
'menu_icon' => 'dashicons-money-alt',
'hierarchical' => false,
'public' => true,
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'taxonomies' => array( 'tax_gifts' ), // Added our taxonomy as "supported" - not required.
'has_archive' => 'gifts', // Defines the post type archive as: '/gifts/'
'rewrite' => array( 'slug' => 'gifts/item' ), // Defines the single post permalink as: '/gifts/item/$post-slug/'
);
// Changed 'gifts' to 'cpt_gifts' to denote that this is a Custom Post Type (CPT).
register_post_type( 'cpt_gifts', $args );
}
add_action('init','gifts_post_type');
// Custom post type GIFTS Taxonomy
function gifts_taxonomy(){
$args = array(
'labels' => array(
'name' => 'Departments',
'singular_name' => 'Department',
),
'public' => true,
'hierarchical' => true,
'show_in_rest' => true, // Needed so that the box show up in the Block Editor
'rewrite' => array( 'slug' => 'gifts' ), // Defines our term archive as: '/gifts/$term-slug/'
);
// Changed 'gifts' to 'tax_gifts' to differentiate the post type from taxonomy.
register_taxonomy( 'tax_gifts', array( 'cpt_gifts' ), $args );
}
add_action('init','gifts_taxonomy');