繁体   English   中英

Woocommerce,使用 wp 查询按类别获取产品

[英]Woocommerce, get products by category with wp query

我尝试了很多方法,但我无法显示带有类别名称或 id 的产品

                  $args = array(
                       'post_type'             => 'product',
                       'post_status'           => 'publish',
                       'ignore_sticky_posts'   => 1,
                       'cat'                   => 40,
                       'posts_per_page'        => '12',
                   );

                  $loop = new WP_Query( $args );

试试这个例子,

因此,给定 ID 为 26 的类别,以下代码将返回其产品(WooCommerce 3+):

 $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => '12', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', //This is optional, as it defaults to 'term_id' 'terms' => 26, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ), array( 'taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too 'operator' => 'NOT IN' ) ) ); $products = new WP_Query($args); var_dump($products);

试试tax_query

$args = array(
   'post_type'             => 'product',
   'post_status'           => 'publish',
   'ignore_sticky_posts'   => 1,
   'posts_per_page'        => '12',
   'tax_query'             => array(
        array(
            'taxonomy'  => 'product_cat',
            'field'     => 'term_id',
            'terms'     => array('40'),
            'operator'  => 'IN',
        )
   )
);

$loop = new WP_Query( $args );

https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

试试这个,

<?php
  $args     = array( 'post_type' => 'product', 'post_status' => 'publish','category' => 34, 'ignore_sticky_posts' => 1, 'posts_per_page' => '12',);
  $products = get_posts( $args ); 
?>

希望这会帮助你。 了解更多信息。

我建议使用 >> wc_get_product_terms( $vars->ID, 'pa_brand_category' )

请检查此屏幕截图 >> http://prntscr.com/hjawu5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM