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

Storefront主题搜索页无结果时无法展示featured products问题咨询

问题原因

你调用的默认[products]短代码会自动继承当前页面的全局查询参数,搜索页的关键词查询条件会被带入短代码的产品查询逻辑中,自然查询不到匹配结果。同时你原来的短代码没有指定featured="true"参数,默认查询全量产品,也不符合你要展示特色产品的需求。

可行解决方案

方案1:短代码适配(改造成本最低)

临时清空全局搜索变量后再调用短代码,执行完成后恢复原有变量,不影响页面其他逻辑,直接替换你在no-product-found.php中的代码即可:

<div class="site-main">
<?php
// 暂存原有搜索关键词
$origin_search_key = get_query_var('s');
// 清空搜索查询变量,避免干扰产品查询
set_query_var('s', '');
// 明确指定查询特色产品,按你需要的数量和列数展示
echo do_shortcode('[products featured="true" per_page="10" columns="3"]');
// 恢复原有搜索关键词,不影响页面其他部分的逻辑
set_query_var('s', $origin_search_key);
?>
</div>

方案2:独立自定义查询(稳定性最高)

完全脱离全局查询参数,自己写查询逻辑拉取特色产品,不会受页面原有任何查询条件影响:

<div class="site-main">
    <h2>推荐特色产品</h2>
    <?php
    // 自定义特色产品查询参数,完全独立于页面原有查询
    $featured_prod_args = [
        'post_type'      => 'product',
        'posts_per_page' => 10,
        'post_status'    => 'publish',
        's'              => '', // 强制清空搜索条件
        'tax_query'      => [
            [
                'taxonomy' => 'product_visibility',
                'field'    => 'name',
                'terms'    => 'featured',
            ]
        ],
        'no_found_rows'  => true, // 无需分页时提升查询性能
    ];
    $featured_query = new WP_Query($featured_prod_args);

    if ($featured_query->have_posts()) {
        // 复用Woocommerce默认产品循环样式,和Storefront主题风格统一
        woocommerce_product_loop_start();
        while ($featured_query->have_posts()) {
            $featured_query->the_post();
            wc_get_template_part('content', 'product');
        }
        woocommerce_product_loop_end();
        // 重置全局post数据,避免污染后续逻辑
        wp_reset_postdata();
    }
    ?>
</div>

注意事项

  • 所有修改建议在Storefront子主题下操作,不要直接修改主主题文件,避免后续主题更新覆盖你的修改
  • 如果不需要严格展示特色产品,只需要展示其他类型产品,调整对应查询参数即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 12:36:01