admin管理员组文章数量:1130349
I have a search form in the home page and search form in the sidebar. Is it possible to make the search form in the home page show results as per a specific template and make the sidebar search form show a different template?
i want to make the homepage search box to show results in different way. Its a custom post. so if user uses that form it should only have data from that custom post. But the default wordpress search box in the sidebar should get the results from entire website.
eg. listing search page results with map
Thanks
I have a search form in the home page and search form in the sidebar. Is it possible to make the search form in the home page show results as per a specific template and make the sidebar search form show a different template?
i want to make the homepage search box to show results in different way. Its a custom post. so if user uses that form it should only have data from that custom post. But the default wordpress search box in the sidebar should get the results from entire website.
eg. listing search page results with map
Thanks
Share Improve this question edited Sep 4, 2018 at 21:34 asked Aug 31, 2018 at 19:21 user145078user145078 5- As in, if you search from the home page, the page you're displaying results on is different from the one in the sidebar? – phatskat Commented Aug 31, 2018 at 19:58
- Now they both have the same template – user145078 Commented Aug 31, 2018 at 20:15
- Do the search forms actually search for anything different? What's the difference, if any, between the forms? – Jacob Peattie Commented Sep 1, 2018 at 2:27
- @JacobPeattie Yes one search is for a custom post and another for default one. – user145078 Commented Sep 1, 2018 at 2:31
- And what are the requests sent by these forms? Can you tell which is which based on GET params? – Krzysiek Dróżdż Commented Sep 1, 2018 at 5:07
1 Answer
Reset to default 2 +50You can restrict a search to a custom post type by modifying a basic WP search form like this:
<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>
To select a specialized template for the custom post type search, add this filter in your functions file:
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');
And of course, you must create the specialized search results template: page_POSTTYPENAME.php
I have a search form in the home page and search form in the sidebar. Is it possible to make the search form in the home page show results as per a specific template and make the sidebar search form show a different template?
i want to make the homepage search box to show results in different way. Its a custom post. so if user uses that form it should only have data from that custom post. But the default wordpress search box in the sidebar should get the results from entire website.
eg. listing search page results with map
Thanks
I have a search form in the home page and search form in the sidebar. Is it possible to make the search form in the home page show results as per a specific template and make the sidebar search form show a different template?
i want to make the homepage search box to show results in different way. Its a custom post. so if user uses that form it should only have data from that custom post. But the default wordpress search box in the sidebar should get the results from entire website.
eg. listing search page results with map
Thanks
Share Improve this question edited Sep 4, 2018 at 21:34 asked Aug 31, 2018 at 19:21 user145078user145078 5- As in, if you search from the home page, the page you're displaying results on is different from the one in the sidebar? – phatskat Commented Aug 31, 2018 at 19:58
- Now they both have the same template – user145078 Commented Aug 31, 2018 at 20:15
- Do the search forms actually search for anything different? What's the difference, if any, between the forms? – Jacob Peattie Commented Sep 1, 2018 at 2:27
- @JacobPeattie Yes one search is for a custom post and another for default one. – user145078 Commented Sep 1, 2018 at 2:31
- And what are the requests sent by these forms? Can you tell which is which based on GET params? – Krzysiek Dróżdż Commented Sep 1, 2018 at 5:07
1 Answer
Reset to default 2 +50You can restrict a search to a custom post type by modifying a basic WP search form like this:
<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>
To select a specialized template for the custom post type search, add this filter in your functions file:
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');
And of course, you must create the specialized search results template: page_POSTTYPENAME.php
本文标签: templatesRestrict a search to a custom post type
版权声明:本文标题:templates - Restrict a search to a custom post type 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749256947a2340640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论