Introduce hierarchical taxonomy URL's, Can be enabled by setting 'hierarchical_url' to true upon taxonomy registration. See #12659

git-svn-id: http://svn.automattic.com/wordpress/trunk@15705 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
dd32 2010-10-04 10:37:25 +00:00
parent 16d7e9e96e
commit d9e1a542ee
2 changed files with 32 additions and 18 deletions

View File

@ -154,21 +154,20 @@ function redirect_canonical($requested_url=null, $do_redirect=true) {
$term_count += count( $tax_query['terms'] );
$obj = $wp_query->get_queried_object();
if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) )
&& !is_wp_error($tax_url) && $redirect['query'] ) {
if ( is_category() ) {
$redirect['query'] = remove_query_arg( array( 'category_name', 'category', 'cat'), $redirect['query']);
} elseif ( is_tag() ) {
$redirect['query'] = remove_query_arg( array( 'tag', 'tag_id'), $redirect['query']);
} elseif ( is_tax() ) { // Custom taxonomies will have a custom query var, remove those too:
$tax = get_taxonomy( $obj->taxonomy );
if ( false !== $tax->query_var)
$redirect['query'] = remove_query_arg($tax->query_var, $redirect['query']);
else
$redirect['query'] = remove_query_arg( array( 'term', 'taxonomy'), $redirect['query']);
if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
if ( !empty($redirect['query']) ) {
if ( is_category() ) {
$redirect['query'] = remove_query_arg( array( 'category_name', 'category', 'cat'), $redirect['query']);
} elseif ( is_tag() ) {
$redirect['query'] = remove_query_arg( array( 'tag', 'tag_id'), $redirect['query']);
} elseif ( is_tax() ) { // Custom taxonomies will have a custom query var, remove those too:
$tax = get_taxonomy( $obj->taxonomy );
if ( false !== $tax->query_var)
$redirect['query'] = remove_query_arg($tax->query_var, $redirect['query']);
else
$redirect['query'] = remove_query_arg( array( 'term', 'taxonomy'), $redirect['query']);
}
}
$tax_url = parse_url($tax_url);
if ( ! empty($tax_url['query']) ) { // Custom taxonomies may only be accessable via ?taxonomy=..&term=..
parse_str($tax_url['query'], $query_vars);
@ -176,7 +175,6 @@ function redirect_canonical($requested_url=null, $do_redirect=true) {
} else { // Taxonomy is accessable via a "pretty-URL"
$redirect['path'] = $tax_url['path'];
}
}
} elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) {
$category = get_term_by('slug', get_query_var('category_name'), 'category');

View File

@ -270,6 +270,7 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
$wp_taxonomies = array();
$defaults = array( 'hierarchical' => false,
'hierarchical_url' => false,
'update_count_callback' => '',
'rewrite' => true,
'query_var' => $taxonomy,
@ -295,7 +296,10 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
'slug' => sanitize_title_with_dashes($taxonomy),
'with_front' => true,
));
$wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
if ( $args['hierarchical'] && $args['hierarchical_url'] )
$wp_rewrite->add_rewrite_tag("%$taxonomy%", '.*?/?([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
else
$wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
$wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front']);
}
@ -2610,16 +2614,28 @@ function get_term_link( $term, $taxonomy = '') {
$termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
$slug = $term->slug;
$t = get_taxonomy($taxonomy);
if ( empty($termlink) ) {
$t = get_taxonomy($taxonomy);
if ( $t->query_var )
$termlink = "?$t->query_var=$slug";
else
$termlink = "?taxonomy=$taxonomy&term=$slug";
$termlink = home_url($termlink);
} else {
$termlink = str_replace("%$taxonomy%", $slug, $termlink);
if ( $t->hierarchical_url ) {
$hierarchical_slugs = array();
$ancestors = get_ancestors($term->term_id, $taxonomy);
foreach ( (array)$ancestors as $ancestor ) {
$ancestor_term = get_term($ancestor, $taxonomy);
$hierarchical_slugs[] = $ancestor_term->slug;
}
$hierarchical_slugs = array_reverse($hierarchical_slugs);
$hierarchical_slugs[] = $slug;
$termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
} else {
$termlink = str_replace("%$taxonomy%", $slug, $termlink);
}
$termlink = home_url( user_trailingslashit($termlink, 'category') );
}
return apply_filters('term_link', $termlink, $term, $taxonomy);