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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try 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