Wordpress - How to change search value using a dro

2019-07-20 18:49发布

As on the title Im loooking to make a search form in wordpress using a select dropdown to search the normal wordpress posts and also a custom post type.

The normal posts has a different look and I have a custom post type wich has vertical thumbnails

I figuret the search results out using this function

function template_chooser($template)
{
  global $wp_query;
  $post_type = get_query_var('post_type');
  if( $wp_query->is_search && $post_type == 'gallery' )
  {
    return locate_template('taxonomy-gallery.php');
  }
  return $template;
}
add_filter('template_include', 'template_chooser');

but my problem is in the search form

the default search form to search default wordpress posts is this one:

<div class="searchbox">
<form action="<?php echo home_url( '/' ); ?>" method="get"><button name="search" class="button">
<span></span></button>
<input type="text" id="s" name="s" value="" />
</form>
</div>

by adding <input type="hidden" name="post_type" value="gallery" /> I can get search results of gallery via taxonomy-gallery.php

so my last search form code is this one

<div class="searchbox">
<form action="<?php echo home_url( '/' ); ?>" method="get"><button name="search" class="button">
<span></span></button>
<input type="text" id="s" name="s" value="" />

<input type="hidden" name="post_type" value="gallery" />


</form>
</div>

but I want to add a select in search input that makes me to choose what I want to search posts or custom post type (gallery)

for example you can check this image

enter image description here

I hope I find any help here thankyou very much.

1条回答
一纸荒年 Trace。
2楼-- · 2019-07-20 19:15

Selection dropdowns can be made this way:

<select name="post_type" >
    <option value="post">Posts</option>
    <option value="gallery">Gallery</option>                 
</select>
查看更多
登录 后发表回答