[wp-hackers] Hierarchical Taxonomies permalinks
Dion Hulse (dd32)
wordpress at dd32.id.au
Tue Jun 15 08:43:10 UTC 2010
Just to confirm, Since the start of custom taxonomies, non-category
hierarchical taxonomies haven't had /parent/child/subchild/ url's (Which
i'm honestly disapointed about).
3.0 hasnt changed that.
3.1 will hopefully change that including support for "$taxonomy__in"
"$taxonomy__not_in" etc.
In the meantime, Here's some code which will give you "category"-link
permalinks for hierarchical custom taxonomies.
http://dd32.id.au/files/wordpress/example-hierarchical-term-links.php
<?php
add_action('init', '_state_taxonomy');
function _state_taxonomy() {
global $wp_rewrite;
register_taxonomy('state', array('note', 'post'), array('hierarchical'
=> true, 'label' => 'States', 'show_ui' => true, 'query_var' => 'state',
'rewrite' => true) );
$wp_rewrite->add_rewrite_tag("%state%", '.*?/?([^/]+)', "state="); //
Override the query var, do this directly after registering the taxonomy
// Note on the regex above, May look strange, but is to only match the
final path item in the url, which is the child slug. WP_Query doesnt
reconise hierarchical custom tax slugs
// $wp_rewrite->flush_rules(); // Flush them on activation, not init,
please?
}
add_filter('term_link', '_hierarchical_terms_hierarchical_links', 9, 3);
function _hierarchical_terms_hierarchical_links($termlink, $term,
$taxonomy) {
global $wp_rewrite;
if ( 'state' != $taxonomy ) // Change 'state' to your required taxonomy
return $termlink;
$termstruct = $wp_rewrite->get_extra_permastruct($taxonomy);
if ( empty($termstruct) ) // If rewrites are disabled, fall back to
the current link
return $termlink;
if ( empty($term->parent) ) // Fall back to the current link for
parent terms
return $termlink;
$nicename = $term->slug;
while ( !empty($term->parent) ) {
$term = get_term($term->parent, $taxonomy);
$nicename = $term->slug . '/' . $nicename;
}
$termlink = str_replace("%$taxonomy%", $nicename, $termstruct);
$termlink = home_url( user_trailingslashit($termlink, 'category') );
return $termlink;
}
Cheers
Dion Hulse / dd32
Contact:
e: contact at dd32.id.au
Web: http://dd32.id.au/
On Tue, 15 Jun 2010 09:53:27 +1000, Ken Newman <WraithKenny at aim.com> wrote:
> Working with a custom Hierarchical Taxonomy, I've noticed that the
> permastructure does not append the parent term to the permalink.
>
> example.com/taxonomy/parent/
> example.com/taxonomy/child/
>
> In a default post category permalink, the parent is appended.
>
> example.com/category/parent/
> example.com/category/parent/child/
>
> Anyone have a way to make custom taxonomies behave more like categories?
More information about the wp-hackers
mailing list