Make WordPress Core

Changeset 15705


Ignore:
Timestamp:
10/04/2010 10:37:25 AM (14 years ago)
Author:
dd32
Message:

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

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/canonical.php

    r15615 r15705  
    155155
    156156            $obj = $wp_query->get_queried_object();
    157 
    158             if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) )
    159                     && !is_wp_error($tax_url) && $redirect['query'] ) {
    160                 if ( is_category() ) {
    161                     $redirect['query'] = remove_query_arg( array( 'category_name', 'category', 'cat'), $redirect['query']);
    162                 } elseif ( is_tag() ) {
    163                     $redirect['query'] = remove_query_arg( array( 'tag', 'tag_id'), $redirect['query']);
    164                 } elseif ( is_tax() ) { // Custom taxonomies will have a custom query var, remove those too:
    165                     $tax = get_taxonomy( $obj->taxonomy );
    166                     if ( false !== $tax->query_var)
    167                         $redirect['query'] = remove_query_arg($tax->query_var, $redirect['query']);
    168                     else
    169                         $redirect['query'] = remove_query_arg( array( 'term', 'taxonomy'), $redirect['query']);
     157            if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
     158                if ( !empty($redirect['query']) ) {
     159                    if ( is_category() ) {
     160                        $redirect['query'] = remove_query_arg( array( 'category_name', 'category', 'cat'), $redirect['query']);
     161                    } elseif ( is_tag() ) {
     162                        $redirect['query'] = remove_query_arg( array( 'tag', 'tag_id'), $redirect['query']);
     163                    } elseif ( is_tax() ) { // Custom taxonomies will have a custom query var, remove those too:
     164                        $tax = get_taxonomy( $obj->taxonomy );
     165                        if ( false !== $tax->query_var)
     166                            $redirect['query'] = remove_query_arg($tax->query_var, $redirect['query']);
     167                        else
     168                            $redirect['query'] = remove_query_arg( array( 'term', 'taxonomy'), $redirect['query']);
     169                    }
    170170                }
    171 
    172171                $tax_url = parse_url($tax_url);
    173172                if ( ! empty($tax_url['query']) ) { // Custom taxonomies may only be accessable via ?taxonomy=..&term=..
     
    177176                    $redirect['path'] = $tax_url['path'];
    178177                }
    179 
    180178            }
    181179        } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) {
  • trunk/wp-includes/taxonomy.php

    r15690 r15705  
    271271
    272272    $defaults = array(  'hierarchical' => false,
     273                        'hierarchical_url' => false,
    273274                        'update_count_callback' => '',
    274275                        'rewrite' => true,
     
    296297            'with_front' => true,
    297298        ));
    298         $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
     299        if ( $args['hierarchical'] && $args['hierarchical_url'] )
     300            $wp_rewrite->add_rewrite_tag("%$taxonomy%", '.*?/?([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
     301        else
     302            $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
    299303        $wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front']);
    300304    }
     
    26112615
    26122616    $slug = $term->slug;
     2617    $t = get_taxonomy($taxonomy);
    26132618
    26142619    if ( empty($termlink) ) {
    2615         $t = get_taxonomy($taxonomy);
    26162620        if ( $t->query_var )
    26172621            $termlink = "?$t->query_var=$slug";
     
    26202624        $termlink = home_url($termlink);
    26212625    } else {
    2622         $termlink = str_replace("%$taxonomy%", $slug, $termlink);
     2626        if ( $t->hierarchical_url ) {
     2627            $hierarchical_slugs = array();
     2628            $ancestors = get_ancestors($term->term_id, $taxonomy);
     2629            foreach ( (array)$ancestors as $ancestor ) {
     2630                $ancestor_term = get_term($ancestor, $taxonomy);
     2631                $hierarchical_slugs[] = $ancestor_term->slug;
     2632            }
     2633            $hierarchical_slugs = array_reverse($hierarchical_slugs);
     2634            $hierarchical_slugs[] = $slug;
     2635            $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
     2636        } else {
     2637            $termlink = str_replace("%$taxonomy%", $slug, $termlink);
     2638        }
    26232639        $termlink = home_url( user_trailingslashit($termlink, 'category') );
    26242640    }
Note: See TracChangeset for help on using the changeset viewer.