1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: Category Slug Hierarchy |
---|
4 | Plugin URI: http://smallvoid.com |
---|
5 | Description: Fixes slugs for taxonomy with hierarchy (Ex. category) |
---|
6 | Author: Rolf Kristensen |
---|
7 | Version: 1.1 |
---|
8 | Author URI: http://smallvoid.com |
---|
9 | */ |
---|
10 | |
---|
11 | function fix_category_slug_hierarchy($q) { |
---|
12 | global $wp_rewrite; |
---|
13 | if (!is_admin() && $wp_rewrite->using_permalinks() && $q->is_main_query() && $q->is_category) |
---|
14 | { |
---|
15 | foreach($q->query as $query) |
---|
16 | { |
---|
17 | $slugs = explode('/', $query); |
---|
18 | if (count($slugs) > 1) |
---|
19 | { |
---|
20 | $category_path = rawurlencode( urldecode( $query ) ); |
---|
21 | $category_path = str_replace( '%2F', '/', $category_path ); |
---|
22 | $category_path = str_replace( '%20', ' ', $category_path ); |
---|
23 | $slugs = '/' . trim( $category_path, '/' ); |
---|
24 | $slugs = explode( '/', $slugs ); |
---|
25 | |
---|
26 | $taxonomy = 'category'; |
---|
27 | $term; |
---|
28 | foreach($slugs as $term_slug) |
---|
29 | { |
---|
30 | if (isset($term)) |
---|
31 | { |
---|
32 | $terms = get_terms($taxonomy, array('parent' => $term->term_id, 'slug' => $term_slug)); |
---|
33 | $term = null; |
---|
34 | if (count($terms)==1) |
---|
35 | $term = $terms[0]; |
---|
36 | else |
---|
37 | break; |
---|
38 | } |
---|
39 | else |
---|
40 | { |
---|
41 | $term = get_term_by('slug', $term_slug, $taxonomy); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | if (isset($term)) |
---|
46 | { |
---|
47 | $tax_query = array( |
---|
48 | array( |
---|
49 | 'taxonomy' => $taxonomy, |
---|
50 | 'terms' => $term->term_id, |
---|
51 | 'field' => 'term_id', |
---|
52 | ) |
---|
53 | ); |
---|
54 | $tax_query = new WP_Tax_Query($tax_query); |
---|
55 | $q->tax_query = $tax_query; |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | if ( function_exists( 'add_action' ) ) |
---|
63 | { |
---|
64 | add_action('parse_tax_query', 'fix_category_slug_hierarchy'); |
---|
65 | } |
---|
66 | ?> |
---|