user1912899
user1912899

Reputation:

WP - Get depth of category's subcategories

In Wordpress, I'm looking to get the depth a category's subcategories.

Suppose I have a category 'grandparent' which has a subcategory 'parent' which in turn has a subcategory 'child'. How do I get to integer 2?

I suppose I could check if 'grandparent' has subcategories and if it does, check if they have subcategories, etc, until I hit 0. But that seems like a lot of unnecessary processing.

Isn't there a more elegant way?

Upvotes: 0

Views: 3279

Answers (3)

There is a nice blog post here that shows you how to build a function that will get you the depth of a category but not only... check the page the function has more options.

EDIT: code here:

function get_depth($id = '', $depth = '', $i = 0) {
    global $wpdb;

    if($depth == '') {
        if(is_page()) {
            if($id == '') {
                global $post;
                $id = $post->ID;
            }
            $depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$id."'");
            return get_depth($id, $depth, $i);
        }
        elseif(is_category()) {
            if($id == '') {
                global $cat;
                $id = $cat;
            }
            $depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$id."'");
            return get_depth($id, $depth, $i);
        }
        elseif(is_single()) {
            if($id == '') {
                $category = get_the_category();
                $id = $category[0]->cat_ID;
            }
            $depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$id."'");
            return get_depth($id, $depth, $i);
        } 
    }
    elseif($depth == '0') {
        return $i;
    }
    elseif(is_single() || is_category()) {
        $depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$depth."'");
        $i++;
        return get_depth($id, $depth, $i);
    }
    elseif(is_page()) {
        $depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$depth."'");
        $i++;
        return get_depth($id, $depth, $i);
    }
}

Upvotes: 2

Krunal Shah
Krunal Shah

Reputation: 2101

Try this one :

$args = array(
                'type'                     => 'post',
                'child_of'                 => 0,
                'parent'                   => 0,
                'orderby'                  => 'name',
                'order'                    => 'ASC',
                'hide_empty'               => 1,
                'hierarchical'             => 0,
                'exclude'                  => '',
                'include'                  => '',
                'number'                   => '',
                'taxonomy'                 => 'category',
                'pad_counts'               => false 
                );
                $cats = get_categories( $args );
                foreach( $cats as $cat) {
                    if($cat->parent == 0) {
                        $parent_cat = null;
                        $head = $cat->name;
                        $head_id = $cat->term_id;
                    }
                    echo "<ul><a class='parent-category' href=''>" . $head . "</a>";                                                    
                    wp_list_cats("sort_column=NAME&optioncount=0&hierarchical=1&hide_empty=0&child_of={$head_id}&show_option_none=");
                    echo "</ul>";
                }

Thanks.

Upvotes: 0

Owen C. Jones
Owen C. Jones

Reputation: 1495

Assuming $cat to be the category ID of 'grandparent':

$number_of_subcategories = count(get_categories("echo=0&cat=" . $cat));

May help?

http://codex.wordpress.org/Function_Reference/get_categories

Upvotes: 0

Related Questions