【问题标题】:Change search result page to Single Page of Custom Post Type将搜索结果页面更改为自定义帖子类型的单页
【发布时间】:2018-12-17 05:10:57
【问题描述】:

我的网站中有两个搜索表单,一个用于blog posts,另一个用于products(woocommerce 产品)。我在这里想要的是我想在相关产品的单页中显示搜索结果,例如Blog Post 搜索表单结果在single-post.phpProduct 搜索结果在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>

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

【问题讨论】:

    标签: wordpress search woocommerce wordpress-theming


    【解决方案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;
    

    【讨论】:

    • 聪明的回答。谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-11-22
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2014-01-03
    相关资源
    最近更新 更多