【问题标题】:Displaying 2nd level of product categories显示第 2 级产品类别
【发布时间】:2019-03-27 12:57:36
【问题描述】:

似乎无法弄清楚在下拉列表中显示 woocommerce 产品类别的第二级的方式。

到目前为止我有这个

function lvl_1()
    {  
        $args = array(
            'show_option_all'    => ' ',
            'name'         => 'Marka',
            'taxonomy'     => 'product_cat',
            'orderby'      => 'name',
            'show_count'   => 0,
            'pad_counts'   => 0,
            'hierarchical' => 1,
            'hide_empty'   => 0,
            'parent'       => 0,
        );
        $lvl_1_categories = wp_dropdown_categories( $args );  

    }
    function lvl_2()
    {
        $args = array(
            'taxonomy'     => 'product_cat',           
            'parent'       => 0,
        );
        $lvl_1_categories = get_categories( $args );  
        $args2 = array(
            'show_option_all'    => ' ',
            'name'         => 'Modelis',
            'taxonomy'     => 'product_cat',
            'orderby'      => 'name',
            'show_count'   => 0,
            'pad_counts'   => 0,
            'hierarchical' => 1,
            'hide_empty'   => 0,  
            // Somewhere here i should get a value of first level elements to dispaly their childer, thats what i think.
            );
        $lvl_2_categories = wp_dropdown_categories( $args2 );  
    }

lvl_1 函数工作正常,它显示顶级类别,但有没有办法用这个wp_dropdown_categories() 函数显示第二级类别?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    放置以下代码并获取所有类别的列表。

    $categories = get_categories( array(
                    'type'        => 'post',
                    'child_of'    => 0,
                    'orderby'     => 'id',
                    'order'       => 'ASC',
                    'hide_empty'  => 0,
                    'hierarchical'=> 0,           
                    'taxonomy'    => 'category',
                    'parent' => 0                                              
                ) );
    
    $top_level_ids = [];
    foreach( $categories as $category ) {
        if( ! $category->parent ) {
            $top_level_ids[] = $category->term_id;
        }
    }
    //print_r($top_level_ids);die;
    $categories = get_categories( array(
                    'type'        => 'post',
                    'child_of'    => 0,
                    'orderby'     => 'id',
                    'order'       => 'ASC',
                    'hide_empty'  => 0,
                    'hierarchical'=> 0,           
                    'taxonomy'    => 'category',
                ) );
    
    foreach( $categories as $category ) {
        // Only output if the parent_id is a TOP level id
        if( in_array( $category->parent, $top_level_ids )) {
            echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />';
        }
    } die;
    

    【讨论】:

    • 我只需要二级分类,不是全部。
    • @Rhercb 请检查上面编辑的代码。它只会获得您的第二级子类别。
    猜你喜欢
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    相关资源
    最近更新 更多