templates - Modifying searchform.php and search.php to have two kinds of searches

I would like to have two search inputs in searchform.php with two different submit buttons. One for searching through po

I would like to have two search inputs in searchform.php with two different submit buttons. One for searching through posts, the other one for categories. Depending on which input is filled, the search.php should display either post results, or category results.

How would I go about this? Do I need two different forms? How do I customise search.php as to display only the relevant results? Do I use a hidden field in the search form? I can handle displaying the results just fine, but how do I get $s and, say, $s2 into search.php? I'm kinda lost as to where to start.

I would like to have two search inputs in searchform.php with two different submit buttons. One for searching through posts, the other one for categories. Depending on which input is filled, the search.php should display either post results, or category results.

How would I go about this? Do I need two different forms? How do I customise search.php as to display only the relevant results? Do I use a hidden field in the search form? I can handle displaying the results just fine, but how do I get $s and, say, $s2 into search.php? I'm kinda lost as to where to start.

Share Improve this question edited Sep 19, 2016 at 10:14 Pim asked Sep 19, 2016 at 10:05 PimPim 1,1521 gold badge11 silver badges26 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

For anyone trying to achieve something similar, here's how I solved it:

In searchform.php, duplicate the form, as such:

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="text" name="s" id="s" placeholder="<?php esc_attr_e( 'Search by Post' ); ?>" />
    <input type="hidden" name="search-type" value="posts" />
    <button type="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search' ); ?>">
    <img src="<?php bloginfo('template_url'); ?>/images/searchic.png" />
    </button>
</form>
<form method="get" id="searchform2" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="text" name="s" id="s2" placeholder="<?php esc_attr_e( 'Search by Category' ); ?>" />
    <input type="hidden" name="search-type" value="categories" />
    <button type="submit" name="submit" id="searchsubmit2" value="<?php esc_attr_e( 'Search' ); ?>">
    <img src="<?php bloginfo('template_url'); ?>/images/searchic.png" />
    </button>
</form>

Then in search.php, below get_header(), the following:

if(isset($_GET['search-type'])) {
    $searchtype = $_GET['search-type'];
    if($searchtype == 'posts') {
        get_template_part( 'search', 'posts' );
    } elseif($searchtype == 'categories') {
        get_template_part( 'search', 'categories' );
    }
}

Then make two files, "search-posts.php" and "search-categories.php" where you can define the loops of the respective search results.

Voilà!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745368766a4624703.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信