我需要创建 products archive page (通常是 WooCommerce 中的 Shop 页面),但显示 ONLY the ON SALE products . 基本上它应该使用与 archive-product.php
中相同的 template layout . 主菜单中将有一个指向此页面的链接 . 我该怎么做?
更新
我设法过滤掉 ON SALE 产品,下面的代码位于 if ( have_posts() ) :
线上方......
$args = array(
'post_type' => 'product',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
query_posts( $args );
代码放在 copy 的 archive-product.php
中,我将其命名为 archive-product_sale.php
并作为 page template .
但是,这仅适用于 Simple products 类型,我需要它适用于 Simple 产品和 Variable 产品类型 .
4 回答
关于短代码的@mirus回答给了我一个想法,看看WooCommerce如何只询问销售商品 . 显然,WooCommerce有一个
wc_get_product_ids_on_sale()
函数,它将返回销售商品的ID . 然后我们可以使用post__in
参数轻松调整查询,仅返回这些特定项目 .WooCommerce在
class-wc-query.php
类中有一个woocommerce_product_query
钩子,它允许我们在运行之前修改查询....它在pre_get_posts
上运行,这是修改查询的通常位置 . 使用Woo的钩子只意味着让它们处理大多数条件逻辑关于应该应用此查询修改的时间 .我设法过滤掉 ON SALE 产品的代码 placed just above
if ( have_posts() ) :
行...代码放在
archive-product.php
的副本中,我将其重命名为archive-product_sale.php
并制作为 page template .@gmaggio使用query_posts()will break your site . 使用pre_get_posts
使用短代码创建新页面
[sale_products per_page="12"]
可用的短代码及其参数列表如下:http://docs.woothemes.com/document/woocommerce-shortcodes/