简体   繁体   English

如何显示基于选定值的搜索表单

[英]How can I display a search form based on a selected value

I have the following function for a custom search in WordPress: 对于WordPress中的自定义搜索,我具有以下功能:

function my_search_form( $form ) {
  $form = '
  <div class="custom-search-dropdown">
    <div class="select-wrapper">
      <select class="blog-store-select">
        <option value="blog">BLOG</option>
        <option value="store">STORE</option>
      </select>
    </div>

    <form role="search" method="get" class="searchform" action="' . home_url( '/' ) . '" >
      <input type="text" value="' . get_search_query() . '" name="s" class="s" placeholder="Site search" />
      <input type="submit" id="searchsubmit" class="search-btn" value="'. esc_attr__( '&#xf002;' ) .'" />
    </form>
  </div>';

    return $form;
}

I have a select dropdown: 我有一个选择下拉列表:

 <select class="blog-store-select">
  <option value="blog">BLOG</option>
  <option value="store">STORE</option>
</select>

What I am trying to do is either use the regular WP search if the Blog option is selected from the dropdown, or if store is selected, grab the value from the search and open a new window with the search parameter passed into a url that I will set. 我想做的是,如果从下拉列表中选择了Blog选项,则使用常规WP搜索,或者如果选择了store ,则从搜索中获取值并打开一个新窗口,并将搜索参数传递给我将设置。

I know I can get the select value using JavaScript with a onChange function - but I am not sure how to handle this inside of php / WordPress. 我知道我可以使用带有onChange函数的JavaScript获得选择值-但我不确定如何在php / WordPress中处理此值。 Any direction, or articles are very much appreciated. 任何方向或文章都非常感谢。

Thank you so much! 非常感谢!

Set the name of your select to post_type , then the value of the select must match the name of the post_type, for example: blog, the post type name is post . 将选择的名称设置为post_type ,然后选择的值必须与post_type的名称匹配,例如:blog,帖子类型的名称为post

Wordpress search query will interpret the post_type var and will only search on the given post_type , WordPress搜索查询将解释post_type var,并且只会在给定的post_type上搜索,

<form role="search" method="get" class="searchform" action="' . home_url( '/' ) . '" >
      <input type="text" value="' . get_search_query() . '" name="s" class="s" placeholder="Site search" />
      <select class="blog-store-select" name="post_type">
        <option value="post">BLOG</option>
        <option value="store">STORE</option>
      <input type="submit" id="searchsubmit" class="search-btn" value="'. esc_attr__( '&#xf002;' ) .'" />
</select>

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

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