最后,我得到了密码。使用下面的代码,您可以选择OnSale 产品Simple Product On Sale 或Variable Product On Sale. 请记住,此代码将选择您在其中添加销售价格的每个帖子。因此,如果您的销售价格与原始价格相同,请避免在此字段中添加价格(如附图所示),因为这样它将不是销售产品,但此代码将在销售产品循环中选择它。
<!-- WooCommerce On-Sale Products -->
<ul class="products">
<?php
$args = array(
\'post_type\' => \'product\',
\'posts_per_page\' => 4,
\'meta_query\' => array(
\'relation\' => \'OR\',
array( // Simple products type
\'key\' => \'_sale_price\',
\'value\' => 0,
\'compare\' => \'>\',
\'type\' => \'numeric\'
),
array( // Variable products type
\'key\' => \'_min_variation_sale_price\',
\'value\' => 0,
\'compare\' => \'>\',
\'type\' => \'numeric\'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( \'content\', \'product\' );
endwhile;
} else {
echo __( \'No products found\' );
}
wp_reset_postdata();
?>
</ul>
<!-- WooCommerce On-Sale Products -->