代码之家  ›  专栏  ›  技术社区  ›  Ramesh Diego Lins de Freitas

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

  •  0
  • Ramesh Diego Lins de Freitas  · 技术社区  · 6 年前

    我的网站有两个搜索表单,一个是 blog posts 另一个是为了 products (WooCommerce产品)。我想在这里显示相关产品的单页搜索结果,如 Blog Post 搜索表单结果 single-post.php Product 搜索结果 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 回复  |  直到 6 年前
        1
  •  1
  •   Pooja Ghetia    6 年前

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