【问题标题】:WooCommerce product category page is blankWooCommerce 产品类别页面为空白
【发布时间】:2019-02-23 04:55:31
【问题描述】:

我有一个自定义存档产品模板,用于在商店首页显示带有标题的产品类别。它还允许我从商店页面隐藏某些类别。这些编辑应用于最新版本的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

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:
     - 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++;
                            }
    

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2014-11-11
      • 2013-02-24
      • 2018-07-23
      • 2016-08-23
      • 1970-01-01
      相关资源
      最近更新 更多