将搜索结果页面更改为自定义post类型的单页

我的网站上有两种搜索形式, 一种用于博客文章, 另一种用于产品(woocommerce产品)。我想要的是我想在相关产品的单个页面中显示搜索结果, 例如在single-post.php中显示Blog Post搜索表单结果, 在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>

如何将搜索结果定向到相关的单个页面?


#1


你可以在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;

来源:

https://www.srcmini02.com/62898.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?