Wordpress 将搜索结果页更改为自定义帖子类型的单个页面

Wordpress 将搜索结果页更改为自定义帖子类型的单个页面,wordpress,search,woocommerce,wordpress-theming,Wordpress,Search,Woocommerce,Wordpress Theming,我的网站上有两个搜索表单,一个用于博客帖子,另一个用于产品(woocommerce产品)。这里我想要的是,我想在相关产品的单个页面中显示搜索结果,如博客文章搜索表单result insingle Post.php和product搜索结果insingle product.php 我已使用隐藏输入字段筛选搜索 <form action="<?php echo get_site_url() ?>" method="GET"> <div class="input-g

我的网站上有两个搜索表单,一个用于
博客帖子
,另一个用于
产品
(woocommerce产品)。这里我想要的是,我想在相关产品的单个页面中显示搜索结果,如
博客文章
搜索表单result in
single Post.php
product
搜索结果in
single product.php

我已使用隐藏输入字段筛选搜索

<form action="<?php echo get_site_url() ?>" method="GET">
    <div class="input-group">
        <input type="search" name="s" class="form-control" placeholder="Search Products" required>
        <input type="hidden" name="post_type" value="products" />
        <div class="input-group-btn">
            <button class="btn btn-default" type="submit">
                 <i class="fa fa-search" aria-hidden="true"></i>
            </button>
        </div>
    </div>
</form>

您可以在search.php中添加这样的代码

 while ( have_posts() ) : the_post();
    if(isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
            if($type == 'products') {
               get_template_part( 'single', 'product' );
            } else {
               get_template_part( 'single', 'post' );
            }
    } else {
            get_template_part( 'single', 'post' );
    }
    endwhile;

聪明的回答。非常感谢。