For all custom taxonomies, the URLs look like: /base/slug/.
I need a solution to for my hierarchical taxonomies to have: /base/parentslug/childslug/.
Anybody knows how to redirect and or to have the same functionality like categories?
Just clarifying, what was pointed out by Parst with a working example of a custom taxonomy registration code:
$labels = array(
'name' => _x( 'Issue numbers', 'taxonomy general name', 'sascha_setup_post_type' ),
'singular_name' => _x( 'Issue number', 'taxonomy singular name', 'sascha_setup_post_type' ),
'search_items' => __( 'Search issues', 'sascha_setup_post_type' ),
'all_items' => __( 'All issue numbers', 'sascha_setup_post_type' ),
'parent_item' => __( 'Year of publication', 'sascha_setup_post_type' ),
'parent_item_colon' => __( 'Year of publication:', 'sascha_setup_post_type' ),
'edit_item' => __( 'Edit issue number', 'sascha_setup_post_type' ),
'update_item' => __( 'Update issue number', 'sascha_setup_post_type' ),
'add_new_item' => __( 'Add new issue number', 'sascha_setup_post_type' ),
'new_item_name' => __( 'New issue number', 'sascha_setup_post_type' ),
'menu_name' => __( 'Issue numbers', 'sascha_setup_post_type' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'issues', 'hierarchical' => true ),
);
register_taxonomy( 'issue_number', array( 'post', 'issue' ), $args );
The result gives a url like www.yourdomain.com/issues/2016/04/ - just as you wanted it to be. And! Don't forget to flush your permalink rules after implementing this, not to get 404 for your custom taxonomy urls. The easiest way - go to Admin / Permalinks and just hit the "save" button, without changing anything.
'hierarchical' => true
to my rewrite solved the issue.
According to register_taxonomy()
documentation there is rewrite/hierarchical
setting which controls that. It defaults to false
, which is probably why you don't see it behaving same as native taxonomies.
hierarchical
argument, this is hierarchical
argument nested inside top-level rewrite
one.
To make it more clear, using rewrite/hierarchical like this "rewrite" => array("hierarchical" => true)
on the $args array will add the parent slug of the taxomomy on the url automatically.
There no necessity to add rules for each term.
function type() {
$labels = array(
"name" => "Type",
"singular_name" => "Type",
"menu_name" => "Type",
);
$args = array(
"labels" => $labels,
"hierarchical" => true,
"public" => true,
"show_ui" => true,
"show_admin_column" => true,
"show_in_nav_menus" => true,
"show_tagcloud" => true,
"show_in_rest" => true,
"rewrite" => array("hierarchical" => true)
);
register_taxonomy("type", array("products"), $args);
}