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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么