【问题标题】:How to change search value using a drop down selector?如何使用下拉选择器更改搜索值?
【发布时间】:2016-03-19 20:02:41
【问题描述】:

正如标题所示,我正在寻找在 WordPress 中使用选择下拉菜单来搜索普通 WordPress 帖子以及自定义帖子类型的搜索表单。

普通帖子有不同的外观,我有一个自定义帖子类型,它有垂直缩略图

我用这个函数计算出搜索结果

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');

但我的问题在于搜索表单

搜索默认 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>

通过添加&lt;input type="hidden" name="post_type" value="gallery" /&gt;,我可以通过taxonomy-gallery.php得到图库的搜索结果

所以我最后一个搜索表单代码是这个

<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>

但我想在搜索输入中添加一个选择,让我可以选择要搜索的帖子或自定义帖子类型(图库)

例如你可以查看这张图片

【问题讨论】:

    标签: javascript php wordpress


    【解决方案1】:

    选择下拉菜单可以这样制作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多