WooCommerce产品类别页面为空白

时间:2019-02-23 04:55:31

标签: php wordpress woocommerce

我有一个自定义的存档产品模板,用于在商店首页上显示带有标题的产品类别。它还使我可以从商店页面中隐藏某些类别。这些编辑已应用于archive-product.php的最新版本。问题是,当我转到特定类别页面时,没有产品显示,只是空白。如果我将归档产品模板恢复为默认值,则将填充产品类别页面。下面是添加到模板中的php,它似乎有问题吗?

谢谢!

<?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 个答案:

答案 0 :(得分:0)

 - 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++;
                        }