简体   繁体   中英

Wordpress - How to change search value using a drop down selector?

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

在此处输入图片说明

I hope I find any help here thankyou very much.

Selection dropdowns can be made this way:

<select name="post_type" >
    <option value="post">Posts</option>
    <option value="gallery">Gallery</option>                 
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM