Reputation: 558
Here are my current URLs for my custom post type and taxonomy. I noticed the rewrite slug for the custom post type and taxonomy cannot both be "inventory" or it breaks. So i went with "inventory-category" and "inventory".
current urls:
category http://localhost:3000/inventory-category/actual-category-name/
single http://localhost:3000/inventory/product-title
I want:
category http://localhost:3000/inventory/actual-category-name/
single http://localhost:3000/inventory/actual-category-name/product-title
So I have 2 questions
How do I get the post type and taxonomy to both share the same url "inventory" instead of using "inventory-category".
How do I get the single to use the actual category name? So it could be:
http://localhost:3000/inventory/cars/product-title
http://localhost:3000/inventory/trucks/product-title
// register a new list of categories for custom post type
function so_inventory_categories() {
$args = array(
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'public' => true,
'publicly_queryable' => true,
'rewrite' => array('with_front' => false, 'slug' => 'inventory-category'),
);
register_taxonomy( 'inventory_categories', array( 'inventory' ), $args );
}
add_action( 'init', 'so_inventory_categories' );
function so_posttype_inventory() {
$args = array(
'labels' => ['name'=>'Inventory'],
'supports' => array( 'title', 'editor' ),
'taxonomies' => array( 'inventory_categories' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'rewrite' => array('with_front' => false,'slug' => 'inventory'),
'menu_icon' => 'dashicons-plus-alt',
);
register_post_type( 'inventory', $args );
}
add_action( 'init', 'so_posttype_inventory', 0 );
Upvotes: 1
Views: 3809
Reputation: 465
You should use rewrite rules to make that work. Something like this:
add_action('init', 'custom_rewrite_basic');
function custom_rewrite_basic() {
add_rewrite_rule('^inventory/([^/]+)/?$', 'index.php?taxonomy=inventory_category&inventory_category=$matches[1]', 'top');
add_rewrite_rule('^inventory/([^/]+)/([^/]+)/?$', 'index.php?post_type=inventory&inventory=$matches[2]', 'top');
}
I haven't tested this so it might still need some tweaking.
Upvotes: 1