Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

If you have a category called "term" and a subcategory called "subterm", your can access subterm posts at /cat/subcat. But this doesn't work out of the box with custom taxonomies. They are accessible at /taxonomy/subterm, but not /taxonomy/term/subterm.

They've gone over this in the Wordpress Trac (http://core.trac.wordpress.org/ticket/12659), and it kind of looks like they have a solution, but as I've never used the Trac and don't fully understand its language (diffs, etc), I need someone more experienced to actually tell me how to implement this. Is there code there that I have to paste into Wordpress core files? Is this already implemented and I can add something to my theme's functions.php?

If possible I'd prefer not to modify any core files. Help!

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

This is implemented in WordPress now as of 3.1.

When you register your taxonomy, make sure to set rewrite hierarchical to true as well as the taxonomy itself:

<?php
register_taxonomy('genre',array('book'), array(
'hierarchical' => true, // this makes it hierarchical in the UI
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'hierarchical' => true ), // this makes hierarchical URLs
));
share|improve this answer
    
Is there a way to add a parameter to an already-registered custom taxonomy? All of my taxonomies were built with a plugin, so it would be nice if I could add something to functions.php that would just add 'rewrite' => array( 'hierarchical' => true ) to those taxonomies. –  supertrue May 15 '11 at 23:06
    
I'm still looking for the answer to this question—see wordpress.stackexchange.com/questions/17444/… –  supertrue May 17 '11 at 15:50
    
It's probably simpler to just add the taxonomies yourself and not use the plugin. –  Milo May 17 '11 at 15:54
    
The problem is I work with others who need the GUI. The hierarchical rewrite parameter is the only thing that many of these plugins still don't support. –  supertrue May 17 '11 at 20:21
    
@supertrue - they need to be able to register taxonomies via a GUI? once you register a taxonomy they will be able to use the GUI to manage terms. just making sure there isn't a confusion here- I've never encountered a situation where new taxonomies needed to be added that regularly. a whole taxonomy, not just terms in the taxonomy. –  Milo May 17 '11 at 22:35
add comment

To make hierarchical permalinks working you need to add rewrite rules. I've managed to assemble some code making a custom post type with a custom taxonomy working with parent/child terms reflected in permalinks. It's not perfect, but it's working!

function keha_add_rewrite_rules() {
        add_rewrite_rule( '^posttype_slug/(.+?)/(.+?)/(.+?)$', 'index.php?taxonomy=$matches[1]&taxonomy=$matches[2]&posttype=$matches[3]', 'top' );
        add_rewrite_rule( '^posttype_slug/(.+?)/(.+?)/$', 'index.php?posttype=$matches[2]', 'top' );
        add_rewrite_rule( '^posttype_slug/(.+?)/(.+?)/(.+?)$', 'index.php?posttype=$matches[3]', 'top' );
        add_rewrite_rule( '^posttype_slug/(.+?)/(.+?)/?$', 'index.php?taxonomy=$matches[2]', 'top' );
        add_rewrite_rule( '^posttype_slug/(.+?)$', 'index.php?taxonomy=$matches[1]', 'top' );
}
add_action('init', 'keha_add_rewrite_rules');

View my working example at Github:

https://github.com/keha76/WordPress-CPT-Hierarchical-Taxonomy-Permalinks

share|improve this answer
add comment

Is there a way to add a parameter to an already-registered custom taxonomy? All of my taxonomies were built with a plugin, so it would be nice if I could add something to functions.php that would just add 'rewrite' => array( 'hierarchical' => true ) to those taxonomies.

You should be able to use register_taxonomy to re-register the taxonomy you want to affect.

http://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/taxonomy.php#L305

WARNING: Untested code!
(Just off the top of my head, mostly. I take no responsibility for broken sites.):

function reregister_taxonomy() {
    # the post types that the taxonomy is registered to
    $post_types = array('post');
    # set this to the taxonomy name
    $tax_name = 'TAXONOMY_NAME_TO_CHANGE';
    # load the already created taxonomy as array so we can
    # pass it back in as $args to register_taxonomy
    $tax = (array)get_taxonomy($tax_name);

    if ($tax) {
        # adjust the hierarchical necessities
        $tax['hierarchical'] = true;
        $tax['rewrite']['hierarchical'] = true;

        # adjust the hierarchical niceties (these could be ignored)
        $tax['labels']['parent_item'] = sprintf(__("Parent %s"),
            $tax->labels->singular_name);
        $tax['labels']['parent_item_colon'] = sprintf(__("Parent %s:"),
            $tax->labels->singular_name);

        # cast caps to array as expected by register_taxonomy
        $tax['capabilities'] = (array)$tax['cap'];
        # cast labels to array
        $tax['labels'] = (array)$tax['labels'];
        # register the taxonomy with our new settings
        register_taxonomy($tax_name, array('post'), $tax);
    }
}
# init action with a late priority so other taxonomies are loaded
# alternatively could be done with the `registered_taxonomy` action hook
add_action('init', 'reregister_taxonomy', 9999);

Only after writing this did I realize this question was a year-and-a-half old, mayhaps somebody will find it useful anyhow.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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