php - wordpress 仅搜索单个类别

标签 php wordpress

我正在索引页面创建一个新闻网站 我按类别列出了所有新闻 我为我的新闻部分之一创建了 1 个类别 现在我想要的是我正在创建一个搜索 该部分的表单仅过滤搜索食谱类别,我该怎么做我想知道基本上我根据 wordpress 标准代码创建了一个普通的 search.php 页面 从主题中过滤新闻。 请帮帮我。

这是表单页面中的代码:

<div class="search_box">
           <form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">  
              <div class="input-group">
                      <input type="text" class="form-control search-bar eng my_control" name="s" id="search" placeholder="<?php echo esc_attr_x( 'Search &hellip;', 'placeholder', 'dastak' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
                      <span class="input-group-btn">
                        <input type="submit" class="search-submit btn my-btn" value="<?php echo esc_attr_x( 'Search', 'submit button', 'dastak' ); ?>">
                        <!-- <button class="btn my-btn"  id="searchsubmit"  type="button"><span class="glyphicon glyphicon-search"></span></button> -->
                      </span>
              </div><!--input group-->

              </form> 
       </div><!--search box-->


<div class="clearfix"></div>    

最佳答案

您可以实现一个函数来捕获搜索,但在 category__in 数组中定义类别。

global $wp_query;
if(is_search()) :
    $cat = array(
        'category__in' => array(5) // Where 5 is the ID of your Music category
    );
$args = array_merge( $wp_query->query, $cat );
endif;
query_post($args);

您可以通过隐藏字段这样做 - 值 22 是类别

<form method="get" id="search form" action="/">
<div>
<input type="text" value="" name="s" id="s" />
<input type="hidden" value="22" name="cat" id="scat" />
<input type="submit" id="search_submit" name="Search" value="Search"/>
</div>
</form>

关于php - wordpress 仅搜索单个类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25358804/

相关文章:

PHP,检测带水印的图像?

php - 删除 WordPress 子主题中的父主题排队文件

WordPress 站点作为 Subversion 工作副本

php - 数据使用$_REQUEST是不是错了?

php - 遍历字符串,删除逗号,添加到 PHP 中的数组

php - Woocommerce - 如何在插件中检查产品类型

php - 如何使用 <use> 加载外部 svg 文件?在 WordPress 中?

css - 在 wordpress 中编辑元素样式

php - PHP 中的 _($string) 函数有什么作用?

php - msqli 不插入