我有一个使用自己的搜索表单的 WP 主题。在下面的代码中可以看到,当搜索表单显示在该类别中时,它已经设置为在该类别中搜索。
我还将搜索表单设置为显示在单个帖子上方,但是当我在此表单中搜索时,我会得到所有类别的结果。我希望搜索表单仅在该帖子的类别中搜索。
我到处搜索,但找不到解决方案。先感谢您!
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<?php // Check to see if category, if yes, then modify the search parameters ?>
<?php if ( is_category() ) : ?>
<input type="hidden" name="cat" value="<?php echo esc_attr( get_query_var( 'cat' ) ); ?>" />
<?php $stext = esc_attr( sprintf( __( 'Search Knowledgebase for %s…', 'ipt_kb' ), single_cat_title( '', false ) ) ); ?>
<?php endif; ?>
<div class="form-group">
<div class="input-group input-group-lg">
<input type="search" class="search-field form-control" placeholder="<?php echo $stext; ?>" value="<?php echo esc_attr( get_search_query() ); ?>" name="s" />
<span class="input-group-btn"><button type="submit" class="btn btn-default"><span class="ipt-icon-search"></span></button></span>
</div>
</div>