Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress WP-获取类别的深度';s子类别_Wordpress_Categories - Fatal编程技术网

Wordpress WP-获取类别的深度';s子类别

Wordpress WP-获取类别的深度';s子类别,wordpress,categories,Wordpress,Categories,在Wordpress中,我希望获得类别子类别的深度 假设我有一个类别“祖父母”,它有一个子类别“父母”,而父类别又有一个子类别“孩子”。如何得到整数2 我想我可以检查“祖父母”是否有子类别,如果有,检查他们是否有子类别,等等,直到我点击0。但这似乎是很多不必要的处理 还有更优雅的方式吗?假设$cat是“祖父母”的类别ID: $number_of_subcategories = count(get_categories("echo=0&cat=" . $cat)); 可以帮忙吗 试试这个

在Wordpress中,我希望获得类别子类别的深度

假设我有一个类别“祖父母”,它有一个子类别“父母”,而父类别又有一个子类别“孩子”。如何得到整数2

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


还有更优雅的方式吗?

假设$cat是“祖父母”的类别ID:

$number_of_subcategories = count(get_categories("echo=0&cat=" . $cat));
可以帮忙吗

试试这个:

$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>";
                }
$args=array(
'type'=>'post',
'child_of'=>0,
“父项”=>0,
'orderby'=>'name',
“订单”=>“ASC”,
“hide_empty”=>1,
“层次结构”=>0,
'排除'=>'',
'包括'=>'',
'编号'=>'',
“分类法”=>“类别”,
“pad_计数”=>错误
);
$cats=获取类别($args);
foreach($cats作为$cat){
如果($cat->parent==0){
$parent_cat=null;
$head=$cat->name;
$head\u id=$cat->term\u id;
}
回声“
    ”; wp_list_cats(“sort_column=NAME&optioncount=0&hide_empty=0&child_of={$head_id}&show_option_none=”); 回声“
”; }
谢谢。

有一篇很好的博客文章,向您展示了如何构建一个函数,让您了解某个类别的深度,而不仅仅是。。。检查页面,该功能有更多选项

编辑:此处的代码:

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);
    }
}

您正在使用哪个功能?谢谢,但它不起作用。它在应该返回2的地方返回1——对于有孩子和孙子的祖父母,它也在应该返回0的地方返回1——一个没有任何子类别的类别——很好,值得一试。我是根据Wordpress的参考文献写的,有时候有点不确定。抱歉,我没有提供帮助。谢谢,但这会输出类别列表,而不是返回子类别的代数。因此,在这种情况下,您只能通过这种方式使用$number\u of_subcategories=count(get\u categories(“echo=0&cat=”.$cat))-1;关于Owen'Coves'Jone的回答