admin管理员组

文章数量:1431032

I'm trying to create a custom theme. I added theme compatibility with Woocommerce to my theme, and am trying to create a product loop. I want to filter my loop so it only displays post with the category "cap" I tried the following versions:

$params = array('posts_per_page' => 5, 'post_type' => 'product', 'category_name' => 'cap');

$wc_query = new WP_Query($params); ?>

and also tried displaying it by Category ID. Both show me no products. Am I doing something wrong? Using PHP 7.2

Some images showing cat setup:

.31.10.png?dl=0

.31.50.png?dl=0

Any help is appreciated!

I'm trying to create a custom theme. I added theme compatibility with Woocommerce to my theme, and am trying to create a product loop. I want to filter my loop so it only displays post with the category "cap" I tried the following versions:

$params = array('posts_per_page' => 5, 'post_type' => 'product', 'category_name' => 'cap');

$wc_query = new WP_Query($params); ?>

and also tried displaying it by Category ID. Both show me no products. Am I doing something wrong? Using PHP 7.2

Some images showing cat setup:

https://www.dropbox/s/97p4hj3e2i1lr4v/Screenshot%202019-04-08%2019.31.10.png?dl=0

https://www.dropbox/s/ybxvsk9da71saoq/Screenshot%202019-04-08%2019.31.50.png?dl=0

Any help is appreciated!

Share Improve this question asked Apr 8, 2019 at 17:32 Pbalazs89Pbalazs89 1151 silver badge12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

woocommerce categories are not a "category" they're the product_cat taxonomy. change your args like so:

$params = array(
    'posts_per_page' => 5,
    'post_type' => 'product',
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'cap'
        )
    )
);

If you use WP_Query then according to the WooCommerce wiki:

Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance.

The best way to create a custom loop is to use wc_get_products() or WC_Product_Query.

In your case, I would do something like this:

$paged                   = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
$ordering                = WC()->query->get_catalog_ordering_args();
$ordering['orderby']     = array_shift(explode(' ', $ordering['orderby']));
$ordering['orderby']     = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
$products_per_page       = 5;// default is apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());

$category_products       = wc_get_products(array(
  'status'               => 'publish',
  'limit'                => $products_per_page,
  'page'                 => $paged,
  'paginate'             => true,
  'return'               => 'ids',
  'orderby'              => $ordering['orderby'],
  'order'                => $ordering['order'],
  'category'             => array('cap')
));

wc_set_loop_prop('current_page', $paged);
wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
wc_set_loop_prop('page_template', get_page_template_slug());
wc_set_loop_prop('per_page', $products_per_page);
wc_set_loop_prop('total', $category_products->total);
wc_set_loop_prop('total_pages', $category_products->max_num_pages);

if($category_products) {
  do_action('woocommerce_before_shop_loop');
  woocommerce_product_loop_start();
    foreach($category_products->products as $featured_product) {
      $post_object = get_post($featured_product);
      setup_postdata($GLOBALS['post'] =& $post_object);
      wc_get_template_part('content', 'product');
    }
    wp_reset_postdata();
  woocommerce_product_loop_end();
  do_action('woocommerce_after_shop_loop');
} else {
  do_action('woocommerce_no_products_found');
}

I actually wrote a post about this with more details here if you're curious or want to extend this further: https://cfxdesign/create-a-custom-woocommerce-product-loop-the-right-way/

本文标签: phpDisplaying Woocommerce Product Category in Wordpress