duanao2585 2019-02-23 04:55
浏览 80
已采纳

WooCommerce产品类别页面为空白

I have a custom archive-product template for displaying my product categories with headers in the shop front page. It also allows me to hide certain categories from the shop page. These edits were applied to the newest version of the archive-product.php. The problem is that when I go to the specific category page, no products are shown, it is just blank. If I revert the archive-product template back to default, the product category pages are then populated. Below is the php added to the template, is there something that seems to be wrong with it?

Thanks!

<?php
if ( woocommerce_product_loop() ) {

    /**
     * Hook: woocommerce_before_shop_loop.
     *
     * @hooked woocommerce_output_all_notices - 10
     * @hooked woocommerce_result_count - 20
     * @hooked woocommerce_catalog_ordering - 30
     */
    do_action( 'woocommerce_before_shop_loop' );

    /* Category - SubCategory START */
                $term           = get_queried_object();
                $parent_id      = empty( $term->term_id ) ? 0 : $term->term_id;

                $excludedCats = array(47);

                $product_categories = get_categories( array( 'taxonomy' => 'product_cat', 'child_of' => $parent_id, 'exclude' => $excludedCats) );

                $i = 1;
                foreach ($product_categories as $product_category) {
                    echo '<h2>'.$product_category->name.'</h2>';
                    woocommerce_product_loop_start(); //open ul

                    $args = array(
                        'posts_per_page' => -1,
                        'tax_query' => array(
                        'relation' => 'AND',
                            array(
                                'taxonomy' => 'product_cat',
                                'field' => 'slug',
                                'terms' => $product_category->slug
                            ),
                        ),
                        'post_type' => 'product',
                        'orderby' => 'menu_order',
                        'order' => 'asc',
                    );
                    $cat_query = new WP_Query( $args );

                    while ( $cat_query->have_posts() ) : $cat_query->the_post();
                        wc_get_template_part( 'content', 'product' );
                    endwhile; // end of the loop.
                wp_reset_postdata();
                woocommerce_product_loop_end(); //close ul
                if ( $i < count($product_categories) )
                    echo '<div class="content-seperator"></div>';
                $i++;
                }//foreach
  • 写回答

1条回答 默认 最新

  • dougu2240 2019-02-23 06:13
    关注
     - Try using following code and let me know if it works
    
    
                    $obj           = get_queried_object();
    
                            if(is_shop()){
                                $parent_id  = empty( $obj->parent  ) ? 0 : $obj->parent;
                            }else if(is_product_category()){
                                $obj  = get_queried_object();
                                $parent_id  = empty( $obj->term_id  ) ? 0 : $obj->term_id; 
                                $cat_slug   = empty( $obj->slug  ) ? 0 : $obj->slug;
    
                            }                
    
                            $excludedCats = array(47);
    
                            if($parent_id != 0){
                                $product_categories = get_terms( array( 'taxonomy' => 'product_cat','slug'=>$cat_slug,'exclude' => $excludedCats) );
    
    
                            }else{
                                $product_categories = get_terms( array( 'taxonomy' => 'product_cat', 'exclude' => $excludedCats) );
    
                            }
    
                            $i = 1;
                            foreach ($product_categories as $product_category) {
                                echo '<h2>'.$product_category->name.'</h2>';
                                woocommerce_product_loop_start(); //open ul
    
                                $args = array(
                                    'posts_per_page' => -1,
                                    'tax_query' => array(
                                    'relation' => 'AND',
                                        array(
                                            'taxonomy' => 'product_cat',
                                            'field' => 'slug',
                                            'terms' => $product_category->slug
                                        ),
                                    ),
                                    'post_parent' => $product_category->parent,
                                    'post_type' => 'product',
                                    'orderby' => 'menu_order',
                                    'order' => 'asc',
                                );
                                $cat_query = new WP_Query( $args );
    
                                while ( $cat_query->have_posts() ) : $cat_query->the_post();
                                    wc_get_template_part( 'content', 'product' );
                                endwhile; 
                            wp_reset_postdata();
                            woocommerce_product_loop_end();
                            if ( $i < count($product_categories) )
                                echo '<div class="content-seperator"></div>';
                            $i++;
                            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!