Woocommerce自定义产品循环在更新后不再起作用

时间:2018-10-17 19:27:10

标签: wordpress loops woocommerce while-loop

我对特色产品的自定义产品循环有问题。在更新之前,它工作正常。

<?php

            $args = array(
                'post_type' => 'product',
                'posts_per_page' => 4,
                'tax_query' => array(
                        array(
                            'taxonomy' => 'product_visibility',
                            'field'    => 'name',
                            'terms'    => 'featured',
                        ),
                    ),
                );

            $loop = new WP_Query( $args );

            while ( $loop->have_posts() ) : $loop->the_post();

                global $product;

                $currency = get_woocommerce_currency_symbol();
                $price = get_post_meta( get_the_ID(), '_regular_price', true);
                $sale = get_post_meta( get_the_ID(), '_sale_price', true);
                if($sale) {
                    echo '
                <div class="pickproductsitem">
                  <div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
                  <div class="prodname">'.get_the_title().'</div>
                  <div class="prodinfo">'. get_the_excerpt() .'</div>
                  <div class="prodprice"><span style="text-decoration: line-through;">Only '.$currency.$price.'</span><span style="color: green;"> SALE '.$currency.$sale.'</span></div><a href="'.get_permalink().'" class="prodbutton w-button">VIEW</a></div>';
                }
                elseif($price) 
                {
                    echo '
                <div class="pickproductsitem">
                  <div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
                  <div class="prodname">'.get_the_title().'</div>
                  <div class="prodinfo">'. get_the_excerpt() .'</div>
                  <div class="prodprice">Only '.$currency.$price.'</div><a href="'.get_permalink().'" class="prodbutton w-button">VIEW</a></div>';
                }

            endwhile;

            wp_reset_query();
        ?>

我已经尝试修复永久链接,但是还没有解决问题。我不是Woo甚至Wordpress的专家,但此循环确实有效。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果要显示特色产品,请在查询参数中使用以下代码,它将解决此问题。

<?php
            $meta_query   = WC()->query->get_meta_query();
            $meta_query[] = array(
                            'key'   => '_featured',
                            'value' => 'yes'
                            );
            $args = array(
                'post_type' => 'product',
                'posts_per_page' => 4,
                'meta_query'  =>  $meta_query
                );

            $loop = new WP_Query( $args );

            while ( $loop->have_posts() ) : $loop->the_post();

                global $product;

                $currency = get_woocommerce_currency_symbol();
                $price = get_post_meta( get_the_ID(), '_regular_price', true);
                $sale = get_post_meta( get_the_ID(), '_sale_price', true);
                if($sale) {
                    echo '
                <div class="pickproductsitem">
                  <div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
                  <div class="prodname">'.get_the_title().'</div>
                  <div class="prodinfo">'. get_the_excerpt() .'</div>
                  <div class="prodprice"><span style="text-decoration: line-through;">Only '.$currency.$price.'</span><span style="color: green;"> SALE '.$currency.$sale.'</span></div><a href="'.get_permalink().'" class="prodbutton w-button">VIEW</a></div>';
                }
                elseif($price) 
                {
                    echo '
                <div class="pickproductsitem">
                  <div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
                  <div class="prodname">'.get_the_title().'</div>
                  <div class="prodinfo">'. get_the_excerpt() .'</div>
                  <div class="prodprice">Only '.$currency.$price.'</div><a href="'.get_permalink().'" class="prodbutton w-button">VIEW</a></div>';
                }

            endwhile;

            wp_reset_query();
        ?>