duanjing4667 2018-06-02 00:26
浏览 162
已采纳

如何按类别woocommerce查询产品

Im building a quick order form/ order table in Woocommerce

What I'm going for is something like this

Main category one
 Subcategory
        product
        product
 Subcategory
        product
        product

Main category two
 Subcategory
        product
        product
 Subcategory
        product
        product

My question is how do I query products under their correct categories

This is what i have so far

<?php

        $custom = array(

                  'post_type' => 'product', 
                  'posts_per_page' => 100,

        ); 

        $cust_query = new WP_Query( $custom );

        if ( $cust_query->have_posts() ) :
            while( $cust_query->have_posts() ) : $cust_query->the_post(); 

            $cat = get_the_terms( get_the_ID(), 'product_cat' ); 


            ?>

        <a href="#" class="link"><?php echo $cat[0]->name; ?></a>
        <div><?php the_title();?></div>



    <?php   endwhile; 
        endif;  

This is querying each category one at a time I want all product that have the same category to be under the correct category with the category only printed once

  • 写回答

1条回答 默认 最新

  • dq62957 2018-06-02 07:22
    关注

    Try with below code. Here i have assumed that you are having one level category only.

    Main category one //level 0 
     Subcategory //level 1
        product
        product
    Subcategory
        product
        product
    
    Main category two //level 0 
    Subcategory //level 1
        product
        product
    Subcategory
        product
        product
    
    
    <ul class="category-sidebar">   
    <?php 
        $get_parent_cats = array(
            'parent' => '0', //get top level categories only
            'taxonomy'=>'product_cat',
            'hide_empty' => false
        ); 
    
        $all_categories = get_categories( $get_parent_cats );//get parent categories 
        foreach( $all_categories as $single_category ){
            //for each category, get the ID
            $catID = $single_category->cat_ID;
    
            echo '<li><a href=" ' . get_category_link( $catID ) . ' ">' . $single_category->name . '</a>'; //category name & link
            $get_children_cats = array(
                'child_of' => $catID, //get children of this parent using the catID variable from earlier
                'taxonomy'=>'product_cat',
                'hide_empty' => false
            );
            wp_reset_postdata();
            $child_cats = get_categories( $get_children_cats );//get children of parent category
            if(count($child_cats)>0){
    
                echo '<ul class="children">';
                    foreach( $child_cats as $child_cat ){
                        //for each child category, get the ID
                        $childID = $child_cat->cat_ID;
    
                        //for each child category, give us the link and name
                        echo '<a href=" ' . get_category_link( $childID ) . ' ">' . $child_cat->name . '</a>';
                        wp_reset_postdata();
                        $args = array(
                                        'post_type' => 'product',
                                        'posts_per_page' => -1,
                                        'tax_query' => array(
                                                            array(
                                                            'taxonomy' => 'product_cat',
                                                            'field' => 'id',
                                                            'terms' => $childID,
                                                            ),
                                                        ),
                                    );
    
                        $loop = new WP_Query($args);
                        echo "<ul>";
                        if($loop->have_posts()) {
                            while ( $loop->have_posts() ) {
                            $loop->the_post();
                            // do something
                            echo "<li><a href=".get_the_permalink()."> Product Name : ".get_the_title()."</a></li>";
                            }
                        }
                        echo "</ul>";
                    }
                echo '</ul></li>';
            }else{
                //$catID
                wp_reset_postdata();
                $args = array(
                    'post_type' => 'product',
                    'posts_per_page' => -1,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'id',
                        'terms' => $catID,
                    ),
                ),
             );
    
             $loop = new WP_Query($args);
            echo "<ul>";
            if($loop->have_posts()) {
                while ( $loop->have_posts() ) {
                $loop->the_post();
                // do something
                echo "<li><a href=".get_the_permalink()."> Product Name : ".get_the_title()."</a></li>";
                }
            }
            echo "</ul>";
            }
        } //end of categories logic ?>
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算