简体   繁体   English

Wordpress-如何使用下拉选择器更改搜索值?

[英]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. 就像标题中的Im希望使用选择下拉列表在正常的wordpress帖子和自定义帖子类型中搜索一样,在wordpress中创建搜索表单。

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: 用于搜索默认wordpress帖子的默认搜索表单是以下一种:

<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 通过添加<input type="hidden" name="post_type" value="gallery" />我可以通过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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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