对于任何试图实现类似目标的人,我是这样解决的:
在searchform中。php,复制表单,如下所示:
<form method="get" id="searchform" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
<input type="text" name="s" id="s" placeholder="<?php esc_attr_e( \'Search by Post\' ); ?>" />
<input type="hidden" name="search-type" value="posts" />
<button type="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( \'Search\' ); ?>">
<img src="<?php bloginfo(\'template_url\'); ?>/images/searchic.png" />
</button>
</form>
<form method="get" id="searchform2" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
<input type="text" name="s" id="s2" placeholder="<?php esc_attr_e( \'Search by Category\' ); ?>" />
<input type="hidden" name="search-type" value="categories" />
<button type="submit" name="submit" id="searchsubmit2" value="<?php esc_attr_e( \'Search\' ); ?>">
<img src="<?php bloginfo(\'template_url\'); ?>/images/searchic.png" />
</button>
</form>
然后在搜索中。php,如下
get_header()
, 以下内容:
if(isset($_GET[\'search-type\'])) {
$searchtype = $_GET[\'search-type\'];
if($searchtype == \'posts\') {
get_template_part( \'search\', \'posts\' );
} elseif($searchtype == \'categories\') {
get_template_part( \'search\', \'categories\' );
}
}
然后创建两个文件,“search posts.php”和“search categories.php”,您可以在其中定义各自搜索结果的循环。
瞧!