首页 > 解决方案 > Woocommerce 自定义产品循环在更新后不再工作

问题描述

我对特色产品的自定义产品循环有疑问。在更新之前它运行良好。

<?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 的专家,但这个循环确实有效。

感谢您的任何帮助。

标签: wordpressloopswoocommercewhile-loop

解决方案


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

<?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();
        ?>

推荐阅读