最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - List just subcategory and products of active category page in Woocommerce

programmeradmin4浏览0评论

Working on a category page - taxonomy-product_cat.php - I want to list all the subcategories of that category page, with all its products. I would think it is simple enough but I cant get it to work.

I can list all categories of the shop with all subcategories and all products - So for example bags would list all products of the whole shop.

Or I can list the subcategories of just the category page - but without the products listed.

How can I list just the subcategories of the category page with their products?

The code I am using that works great, but loops all the categories in the shop, and not just the active category is:

$args = array('taxonomy' => 'product_cat' ); $all_categories = get_categories( $args );

foreach ($all_categories as $cat) {
    $category_id = $cat->term_id;
    $args2 = array('taxonomy' => $taxonomy,'parent' => $category_id,'hierarchical' => $hierarchical, 'orderby' => $orderby, 'order' => $order,'hide_empty' => $empty); $categories = get_categories( $args2 );


$sub_cats = get_categories( $args2 );
            if($sub_cats) {
                foreach($sub_cats as $sub_category) {
                    echo "<h2>".$sub_category->cat_name."</h2>";

                    $args = array( 'post_type' => 'product','product_cat' => $sub_category->slug, 'orderby' => $orderby, 'order' => $order);
                            $loop = new WP_Query( $args );   
                            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>


                            <!--HTML HERE-->


                            <?php endwhile;  wp_reset_query();  }}} ?>

Working on a category page - taxonomy-product_cat.php - I want to list all the subcategories of that category page, with all its products. I would think it is simple enough but I cant get it to work.

I can list all categories of the shop with all subcategories and all products - So for example bags would list all products of the whole shop.

Or I can list the subcategories of just the category page - but without the products listed.

How can I list just the subcategories of the category page with their products?

The code I am using that works great, but loops all the categories in the shop, and not just the active category is:

$args = array('taxonomy' => 'product_cat' ); $all_categories = get_categories( $args );

foreach ($all_categories as $cat) {
    $category_id = $cat->term_id;
    $args2 = array('taxonomy' => $taxonomy,'parent' => $category_id,'hierarchical' => $hierarchical, 'orderby' => $orderby, 'order' => $order,'hide_empty' => $empty); $categories = get_categories( $args2 );


$sub_cats = get_categories( $args2 );
            if($sub_cats) {
                foreach($sub_cats as $sub_category) {
                    echo "<h2>".$sub_category->cat_name."</h2>";

                    $args = array( 'post_type' => 'product','product_cat' => $sub_category->slug, 'orderby' => $orderby, 'order' => $order);
                            $loop = new WP_Query( $args );   
                            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>


                            <!--HTML HERE-->


                            <?php endwhile;  wp_reset_query();  }}} ?>
Share Improve this question asked Jan 20, 2020 at 16:43 JonJon 3652 gold badges8 silver badges24 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The code below works for me in archive-product.php not sure if it may also work in taxonomy-product_cat.php

EDIT I've added some lines of code to exclude posts previously outputted, since a post can belong to several categories and that's why you can find always the same posts

    if(is_a(get_queried_object(),'WP_term')){
    $subs=get_terms('product_cat',array('parent'=>get_queried_object()->term_id));
    //var_dump($subs);
    $to_excude=[];
    foreach($subs as $sub){
        $args = array(
            'post_type' => 'product',
            'post_status'=>'publish',
            'post__not_in' =>$to_excude,
            'tax_query' => array(
                    array(
                            'taxonomy' => 'product_cat',
                            'field'    => 'term_id',
                            'terms'    => $sub->slug
                    ),
            ),
    );
    $query = new WP_Query( $args );
    if($query->have_posts()):
        ?>
        <h4><?php echo $sub->name?></h4>
        <?php
        while($query->have_posts()):
            //
            $to_excude[]=get_the_ID();
            $query->the_post();
            the_title();
            // etc
        endwhile;
    endif;
    wp_reset_query();   
    }

}

I cobbled this code together from different ideas online, and seems to work:

<?php foreach ( $product_categories as $product_category ) {
    echo '<h2><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h2>';      
    $args = array(
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $product_category->slug
            )
        ),
        'post_type' => 'product',            
        'orderby' => 'name',
        'order'   => 'ASC', 
        'posts_per_page' => -1 

    );      

    $products = new WP_Query( $args );      
    while ( $products->have_posts() ) {
        $products->the_post();    ?>

        //HTML CODE HERE
 <div>  <?php the_content(); ?> </div>
 <?php  }  } ?>

However I am having some issues ordering the outputted products - if anyone has any ideas. Changing the 2nd args 'order' => 'DESC' or adding 'meta_key' => 'size', 'orderby' => 'meta_value_num' orderby` etc does not seem to affect the product order listing under the sun category titles.

发布评论

评论列表(0)

  1. 暂无评论