You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用WooCommerce短代码仅显示特定分类的促销商品?

解决WooCommerce仅展示指定分类促销商品的问题

嘿,这个需求其实挺常见的,我来给你几个实用的解决思路,从简单到灵活都有:

方法1:用内置参数直接搞定(最省心)

WooCommerce自带的sale_products短代码其实支持分类筛选参数,完全不用额外写代码就能实现需求。你可以传入分类的slug(别名)或者ID,多个分类用逗号分隔就行。

举几个具体例子:

  • 如果目标分类的slug是home-appliances,短代码改成这样:
    [sale_products per_page="32" category="home-appliances"]
    
  • 要是你知道分类ID(比如ID是8),就写成:
    [sale_products per_page="32" category="8"]
    
  • 想同时展示多个分类的促销商品?直接用逗号分隔:
    [sale_products per_page="32" category="home-appliances,8"]
    

方法2:自定义短代码(应对复杂场景)

如果内置参数满足不了你的特殊需求(比如要排除子分类、自定义排序规则等),可以自己写一个自定义短代码。把下面这段代码放到主题的functions.php文件或者自定义插件里就行:

function custom_category_sale_products_shortcode($atts) {
    // 设置默认参数,可根据需求调整
    $args = shortcode_atts(array(
        'category_id' => '',
        'per_page' => 32,
        'orderby' => 'date',
        'order' => 'desc'
    ), $atts);

    // 验证分类ID是否填写
    if (empty($args['category_id'])) {
        return '<p>请指定目标分类的ID</p>';
    }

    // 构建促销商品的查询参数
    $product_query = array(
        'post_type' => 'product',
        'posts_per_page' => $args['per_page'],
        'orderby' => $args['orderby'],
        'order' => $args['order'],
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id',
                'terms' => $args['category_id'],
                'operator' => 'IN'
            ),
            // 排除隐藏商品
            array(
                'taxonomy' => 'product_visibility',
                'field' => 'name',
                'terms' => 'exclude-from-catalog',
                'operator' => 'NOT IN'
            )
        ),
        'meta_query' => array(
            'relation' => 'OR',
            // 匹配正在进行的简单促销
            array(
                'key' => '_sale_price',
                'value' => 0,
                'compare' => '>',
                'type' => 'numeric'
            ),
            // 匹配定时促销的商品
            array(
                'key' => '_sale_price_dates_from',
                'value' => current_time('timestamp'),
                'compare' => '<='
            ),
            array(
                'key' => '_sale_price_dates_to',
                'value' => current_time('timestamp'),
                'compare' => '>='
            )
        )
    );

    $products = new WP_Query($product_query);

    // 输出商品列表
    ob_start();
    if ($products->have_posts()) {
        woocommerce_product_loop_start();
        while ($products->have_posts()) {
            $products->the_post();
            wc_get_template_part('content', 'product');
        }
        woocommerce_product_loop_end();
        wp_reset_postdata();
    } else {
        echo '<p>该分类暂无促销商品</p>';
    }
    return ob_get_clean();
}
add_shortcode('category_sale_products', 'custom_category_sale_products_shortcode');

使用这个自定义短代码时,只需要传入分类ID和每页数量:

[category_sale_products category_id="8" per_page="32"]

方法3:可视化插件(适合不想写代码的用户)

要是你完全不想碰代码,可以用WooCommerce的筛选类插件,比如WooCommerce Product Filters这类工具。它们支持可视化设置促销商品的分类筛选规则,然后直接生成短代码或者添加到菜单里,操作起来很直观。


内容的提问来源于stack exchange,提问作者Najd Dergham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:14:40