WP - 获取类别子类别的深度

时间:2013-10-02 12:09:52

标签: wordpress categories

在Wordpress中,我希望深度是类别的子类别。

假设我有一个类别'祖父母',它有一个子类别'parent',而子类别又有一个子类别'child'。我如何得到整数2?

我想我可以检查'祖父母'是否有子类别,如果有,检查他们是否有子类别等,直到我达到0.但这似乎是很多不必要的处理。

是不是有更优雅的方式?

3 个答案:

答案 0 :(得分:2)

有一篇不错的博客文章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);
    }
}

答案 1 :(得分:0)

假设$ cat为'祖父母'的类别ID:

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

可以帮忙吗?

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

答案 2 :(得分:0)

试试这个:

$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>";
                }

感谢。

相关问题