Make WordPress Core

Ticket #5034: category-slug-hierarchy.php

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