如何使用下拉选择器更改搜索值? [英] How to change search value using a drop down selector?
问题描述
正如标题一样,我希望使用选择下拉列表在 WordPress 中创建一个搜索表单来搜索普通的 WordPress 帖子和自定义帖子类型.
普通帖子有不同的外观,我有一个自定义帖子类型,其中包含垂直缩略图
我用这个函数算出搜索结果
function template_chooser($template){全局 $wp_query;$post_type = get_query_var('post_type');if( $wp_query->is_search && $post_type == 'gallery' ){返回 locate_template('taxonomy-gallery.php');}返回 $template;}add_filter('template_include', 'template_chooser');
但我的问题在于搜索表单
搜索默认 WordPress 帖子的默认搜索表单是这样的:
<form action="<?php echo home_url( '/' ); ?>"method="get">
通过添加<input type="hidden" name="post_type" value="gallery"/>
我可以通过taxonomy-gallery.php获取图库的搜索结果
所以我最后的搜索表单代码是这个
<form action="<?php echo home_url( '/' ); ?>"method="get">
但我想在搜索输入中添加一个选择,让我选择我想要搜索的帖子或自定义帖子类型(图库)
例如你可以查看这张图片
选择下拉菜单可以这样进行:
As on the title I'm 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 which 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
Selection dropdowns can be made this way:
<select name="post_type" >
<option value="post">Posts</option>
<option value="gallery">Gallery</option>
</select>
这篇关于如何使用下拉选择器更改搜索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!