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

WP主题内置搜索显文章商品 仅保留商品缩略图视图修改方法

结论

修改主题search.php文件完全可以实现需求,不需要更换现有内置搜索引擎,核心要做两个调整:一是过滤搜索结果,仅返回商品类型内容;二是替换搜索结果的渲染模板,复用商品分类页的缩略图视图组件。

具体修改方法

1. 限定搜索结果只返回商品

你当前的代码直接调用全局搜索查询的结果,默认会包含文章、页面、商品等所有公开内容类型。在文件最开头、所有输出逻辑之前加入如下代码,强制当前搜索查询只拉取WooCommerce商品内容:

<?php
global $wp_query;
// 限定搜索仅返回商品
$wp_query->set('post_type', 'product');
// 重新执行查询获取过滤后的结果
$wp_query->get_posts();
?>

2. 替换结果渲染模板,复用分类页缩略图视图

你现有代码在循环中调用的是搜索专用的通用内容模板template-parts/content/search,这个模板是为文章类内容设计的,需要替换为商品分类页使用的商品卡片模板:

  • 首先将循环内的get_template_part( 'template-parts/content/search');替换为WooCommerce官方商品模板调用代码:
    wc_get_template_part( 'content', 'product' );
    
  • 其次给商品列表加上和分类页一致的外层容器,避免样式错位。在/* Start the Loop */注释下方、while ( have_posts() ) : the_post();之前加入商品列表容器起始标签,在endwhile;之后加入容器闭合标签,列数可以和你分类页设置的保持一致(常见为3列或4列):
    <ul class="products columns-3">
    <?php
    /* Start the Loop */
    while ( have_posts() ) : the_post();
        wc_get_template_part( 'content', 'product' );
    endwhile;
    ?>
    </ul>
    

如果替换后样式和分类页有差异,可以打开主题目录下的woocommerce/archive-product.php文件,复制商品分类页循环部分的外层容器class,替换上面的products columns-3类名即可完全匹配样式。

修改后完整参考代码

<?php
// 搜索结果仅查询商品
global $wp_query;
$wp_query->set('post_type', 'product');
$wp_query->get_posts();
?>

<?php if ( have_posts() ) : ?>

<header>
    <h1 class="selleradise_page__title">
        <?php
            printf(
                /* translators: %s: Search Term. */
                esc_html( __("Search Results for: %s", "selleradise-lite") ),
                '<em>' . get_search_query() . '</em>'
            );
        ?>
    </h1>
</header><!-- .page-header -->

<main id="main" class="site-main" role="main">

    <?php
    ?>
    <ul class="products columns-3">
    <?php
    /* Start the Loop */
    while ( have_posts() ) : the_post();
        wc_get_template_part( 'content', 'product' );
    endwhile;
    ?>
    </ul>
        
    <?php
    if($wp_query->max_num_pages > 1): ?>

        <div class="selleradise_pagination">

            <?php
                $args = [
                    'total'     => $wp_query->max_num_pages,
                    'current'   => max( 1, get_query_var( 'paged' ) ),
                    'format'    => '?paged=%#%',
                    'show_all'  => false,
                    'type'      => 'array',
                    'end_size'  => 0,
                    'mid_size'  => 2,
                    'prev_next' => true,
                    'prev_text' => selleradise_svg('unicons-line/angle-left-b'),
                    'next_text' => selleradise_svg('unicons-line/angle-right-b'),
                    'add_args'  => false,
                    'add_fragment' => '',
                    'aria_current' => "page",
                ];

                echo paginate_links($args);
            ?>
        </div>

    <?php endif; 

        else :

        get_template_part( 'template-parts/content/none');

        endif;
    ?>

</main><!-- #main -->
</div><!-- #primary -->

注意事项

  • 修改前建议备份原search.php文件,避免修改出错无法恢复
  • 如果后续主题更新会覆盖模板修改,建议将修改后的文件放到子主题对应目录下,改动就不会丢失

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 23:27:29