我们如何在分类页面上显示产品的类别标题,但在Woocommerce中不包括父产品?

时间:2020-01-23 07:31:33

标签: php wordpress woocommerce hook-woocommerce woocommerce-bookings

我有如下类别模式:

父母

  • 孩子1
  • 孩子2

当我单击“父级存档”页面时,有什么方法可以分别显示所有子级产品及其类别,但父级除外。我已经尝试了很多代码,但是它也给出了父类别和产品,但我只需要它是孩子。这是代码:-

foreach( get_terms( array( 'taxonomy' => 'product_cat' ) ) as $category ) :
    $products_loop = new WP_Query( array(
        'post_type' => 'product',
        'showposts' => -1,
        'hierarchical' => 1,
        'depth' => 1,
        'parent' => 0,
        'tax_query' => array_merge( array(
            'relation'  => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'terms'    => array( $category->term_id ),
                'field'   => 'term_id'
            )
        ), WC()->query->get_tax_query() ),

        'meta_query' => array_merge( array(

            // You can optionally add extra meta queries here

        ), WC()->query->get_meta_query() )
    ) );
?>
    <h2 class="category-title"><?php echo $category->name; ?></h2>
    <?php
    while ( $products_loop->have_posts() ) {
        $products_loop->the_post();
        do_action( 'woocommerce_shop_loop' );
        // wc_get_template_part( 'content', 'product' );
    }
    wp_reset_postdata(); ?>

0 个答案:

没有答案