WooCommerce showing active product categories on single product page

I'm using this code to show a list of product child-categories of a specific category (Parent ID = 173) on my sideb

I'm using this code to show a list of product child-categories of a specific category (Parent ID = 173) on my sidebar on a single product page.

<?php
    $orderby = 'name';
    $order = 'asc';
    $hide_empty = false ;
    $cat_args = array(
                'parent'     => 173,
                'orderby'    => $orderby,
        'order'      => $order,
        'hide_empty' => $hide_empty,

    );
    $product_categories = get_terms( 'product_cat', $cat_args );
    if( !empty($product_categories) ){
        echo '<ul>';
        foreach ($product_categories as $key => $category) {
            echo '<li>';
            echo '<a href="'.get_term_link($category).'" >';
            echo $category->name;
            echo '</a>';
            echo '<li>';
        }
        echo '</ul>';
    }
?>

However it's showing ALL child-categories that exist on my website. I would like to just show the active ones that this product is in. How can I tweak this to just show the active ones?

I'm using this code to show a list of product child-categories of a specific category (Parent ID = 173) on my sidebar on a single product page.

<?php
    $orderby = 'name';
    $order = 'asc';
    $hide_empty = false ;
    $cat_args = array(
                'parent'     => 173,
                'orderby'    => $orderby,
        'order'      => $order,
        'hide_empty' => $hide_empty,

    );
    $product_categories = get_terms( 'product_cat', $cat_args );
    if( !empty($product_categories) ){
        echo '<ul>';
        foreach ($product_categories as $key => $category) {
            echo '<li>';
            echo '<a href="'.get_term_link($category).'" >';
            echo $category->name;
            echo '</a>';
            echo '<li>';
        }
        echo '</ul>';
    }
?>

However it's showing ALL child-categories that exist on my website. I would like to just show the active ones that this product is in. How can I tweak this to just show the active ones?

Share Improve this question asked Jun 30, 2019 at 19:56 SeanAUS120SeanAUS120 311 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Most importantly - hide_empty => true Look in the codex for possible args for get_terms: https://developer.wordpress/reference/classes/wp_term_query/__construct/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356518a4624152.html

相关推荐

  • WooCommerce showing active product categories on single product page

    I'm using this code to show a list of product child-categories of a specific category (Parent ID = 173) on my sideb

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信