通过修改基本WP搜索表单,可以将搜索限制为自定义帖子类型,如下所示:
<form id="cptsearch" action="<?php echo home_url(); ?>" method="get">
<input type="text" name="s" />
<input type="hidden" name="post_type" value="POSTTYPENAME" />
<input id="searchsubmit" type="submit" alt="Search" value="Search" />
</form>
要为自定义帖子类型搜索选择专用模板,请在函数文件中添加此过滤器:
function template_chooser($template) {
global $wp_query;
$post_type = get_query_var(\'post_type\');
if( $wp_query->is_search && $post_type == \'POSTTYPENAME\' ) {
return locate_template(\'page_POSTTYPENAME.php\');
}
return $template;
}
add_filter(\'template_include\', \'template_chooser\');
当然,您必须创建专门的搜索结果模板:page\\u POSTTYPENAME。php