3

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?

3 Answers 3

4

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.

1
  • This is the answer. Adding 'hierarchical' => true to my rewrite solved the issue.
    – robrecord
    Jun 22, 2022 at 12:26
3

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.

2
  • Thanks for your note. But I already set the hierarchical parameter as "true" in the register taxonomy settings. The URL is still not using the parentslug.
    – Traveler
    Jul 21, 2014 at 19:53
  • 1
    @Traveler hmmm... flushed permalinks after? Note that this is not top-level hierarchical argument, this is hierarchical argument nested inside top-level rewrite one.
    – Rarst
    Jul 21, 2014 at 20:10
0

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.