如何在类别页面上显示产品子类别

时间:2018-08-20 12:48:14

标签: php wordpress woocommerce

我想在woocomerce wordpress网站上单击产品类别,然后先向我显示子类别,然后再向我展示产品...

类似的东西。

     <?php if (is_category()) { 
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id
&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.
"&echo=0"); else
$this_category = wp_list_categories('orderby=id&depth=1
&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.
"&echo=0");
if ($this_category) { ?> 

<ul>
<?php echo $this_category; ?>

</ul>

<?php } ?>

1 个答案:

答案 0 :(得分:0)

将此添加到主题的“ functions.php”中。

function show_product_subcategories( $args = array() ) {    
    if(!is_shop()) //not shop page
    {
        $parentid = get_queried_object_id();

        $args = array(
            'parent' => $parentid,
            'hide_empty' => false,
        );
        $terms = get_terms( 'product_cat', $args );
        if ( $terms ) {
            echo '<div class="sub_categories">
                <ul class="product-cats cat_products_list">';
                foreach ( $terms as $category ) {
                    wc_get_template('content-product_cat.php',array('category'=>$category));
                }
            echo '</ul><div class="clearfix"></div></div>';
        }
    }
}
add_action( 'woocommerce_before_shop_loop', 'show_product_subcategories', 50 );