WordPress>显示所有类别,子类别和子子类别

时间:2017-11-08 23:19:44

标签: php wordpress

我试图使用php在WordPress网站上显示所有类别和子类别,如下所示:

  • 类别
    • 子类别
      • 子类别
    • 子类别
      • 子类别
      • 子类别

问题是到目前为止我只能获得类别和子类别部分,但我无法获得子子类别:(

以下是我使用的代码(我在stackoverflow上找到它但它没有显示子子类:():

<?php

    $taxonomy     = 'product_cat';
    $orderby      = 'name';  
    $show_count   = 0;      
    $pad_counts   = 0;      
    $hierarchical = 1;      
    $title        = '';  
    $empty        = 0;

    $args = array(
        'taxonomy'     => $taxonomy,
        'orderby'      => $orderby,
        'show_count'   => $show_count,
        'pad_counts'   => $pad_counts,
        'hierarchical' => $hierarchical,
        'title_li'     => $title,
        'hide_empty'   => $empty
    );

    $all_categories = get_categories( $args );

    foreach ($all_categories as $cat) {

        if($cat->category_parent == 0) {

            $category_id = $cat->term_id;

            echo '<br /> ('. $category_id .') <a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

            $args2 = array(
                'taxonomy'     => $taxonomy,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
            );

            $sub_cats = get_categories( $args2 );

            if($sub_cats) {

                foreach($sub_cats as $sub_category) {
                    echo  '<br/> > <a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
                    echo apply_filters( 'woocommerce_subcategory_count_html', ' (' . $sub_category->count . ')', $category );
                }
            }
        }       
    }
?>

我认为我需要获得子类别ID以便能够列出所有子子类别,但问题是我无法找到获取子类别ID的方法...到目前为止我只能获得类别ID :(

0 个答案:

没有答案