【发布时间】:2021-08-04 22:45:38
【问题描述】:
我的 wordpress 网站上有两个表单,search.blade.php 和 blog-search.blade.php。
search.blade.php 位于网站标题中,搜索所有内容类型。
blog-search.blade.php 仅用于搜索帖子类型。
我正在使用以下代码让它只搜索博客文章:
function searchfilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
可以理解,这适用于 search.blade.php 和 blog-search.blade.php,但我只希望它适用于 blog-search。
我认为如果我添加一个条件来检查仅存在于博客搜索中的隐藏输入的值,我可以让它工作,但我不知道该怎么做。
这是我的 blog-search.blade.php 代码:
<form action="{{ get_bloginfo('url') }}" method="GET" class="blog-search-form">
<input type="search" name="s" placeholder="Search the blog...">
<input type="hidden" name="search-type" value="normal" />
<span class="icon-search"></span>
</form>
有没有办法实现类似的东西:
function searchfilter($query) {
if ($query->is_search && !is_admin() && INPUT TYPE == HIDDEN ) {
$query->set('post_type', 'post');
}
...
所以只有博客搜索搜索帖子,而我的主要搜索照常工作?我尝试了implementing this,但没有成功,所以我认为条件可以提供帮助。
search.blade.php,以防万一:
<form action="{{ get_bloginfo('url') }}" method="GET" class="search-form">
<input type="search" name="s" placeholder="Search the site">
</form>
【问题讨论】:
标签: php wordpress forms query-string query-variables