admin管理员组文章数量:1122832
I know this is asked a million times, and I've found the solution. It seems fairly straight forward but it's not working for some reason. I wonder if it has something to do with the fact that I'm performing the search using this plugin: / but I have my doubts. I was hoping someone had some insight, maybe I'm missing something. For reference here is my code:
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'product' )
{
return locate_template('archive-products.php');
} else {
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );
}
return $template;
}
add_filter('template_include', 'template_chooser');
I'm using this with WooCommerce, the search and filter plugin is to select products by category, attribute, and by name. The search does work it just does not pull in the custom template. What I'm trying to do is create a custom results page to display the products in the same way that they are displayed on a WooCommerce category page. For some reason if you do a search without selecting anything it'll use the standard results template that's used for post, pages, etc. and not the WooCommerce product template. If you select a category or attribute it'll display the WooCommerce product template as it should with search results.
Any help would be very much appreciated. Thank you.
I know this is asked a million times, and I've found the solution. It seems fairly straight forward but it's not working for some reason. I wonder if it has something to do with the fact that I'm performing the search using this plugin: https://wordpress.org/plugins/search-filter/ but I have my doubts. I was hoping someone had some insight, maybe I'm missing something. For reference here is my code:
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'product' )
{
return locate_template('archive-products.php');
} else {
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );
}
return $template;
}
add_filter('template_include', 'template_chooser');
I'm using this with WooCommerce, the search and filter plugin is to select products by category, attribute, and by name. The search does work it just does not pull in the custom template. What I'm trying to do is create a custom results page to display the products in the same way that they are displayed on a WooCommerce category page. For some reason if you do a search without selecting anything it'll use the standard results template that's used for post, pages, etc. and not the WooCommerce product template. If you select a category or attribute it'll display the WooCommerce product template as it should with search results.
Any help would be very much appreciated. Thank you.
Share Improve this question asked Sep 23, 2014 at 14:21 jasenmpjasenmp 1331 silver badge5 bronze badges1 Answer
Reset to default 0Try adding the return as well in the else{}
, like this:
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'product' )
{
return locate_template('archive-products.php');
} else {
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );
return locate_template('archive-products.php');
}
return $template;
}
add_filter('template_include', 'template_chooser');
Haven't tested.
本文标签: filtersCustom Search Template for Custom Post Types
版权声明:本文标题:filters - Custom Search Template for Custom Post Types 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288054a1928013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论