wordpress 只搜索单个类别

Abh*_*bhi 1 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>    
Run Code Online (Sandbox Code Playgroud)

jag*_*itg 5

您可以实现一个函数来捕获搜索,但在 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);
Run Code Online (Sandbox Code Playgroud)

或者

您可以通过隐藏字段这样做 - 值 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>
Run Code Online (Sandbox Code Playgroud)