Get WooCommerce product categories from WordPress

前端 未结 4 1078
甜味超标
甜味超标 2020-11-30 18:30

I am trying to get the product categories from WooCommerce through a function in my WordPress theme

    function get_me_list_of($atts, $content = null)
             


        
4条回答
  •  庸人自扰
    2020-11-30 18:57

    Improving Suman.hassan95's answer by adding a link to subcategory as well. Replace the following code:

    $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo  $sub_category->name ;
            }
    
        }
    

    with:

    $sub_cats = get_categories( $args2 );
                if($sub_cats) {
                    foreach($sub_cats as $sub_category) {
                        echo  '
    '. $sub_category->name .''; } }

    or if you also wish a counter for each subcategory, replace with this:

    $sub_cats = get_categories( $args2 );
                if($sub_cats) {
                    foreach($sub_cats as $sub_category) {
                        echo  '
    '. $sub_category->name .''; echo apply_filters( 'woocommerce_subcategory_count_html', ' ' . $sub_category->count . '', $category ); } }

提交回复
热议问题