我使用以下代码获取我的wordpress网站中的woocommerce的产品类别列表:
<?PHP $taxonomy = 'product_cat'; $orderby = 'name'; $show_count = 0; // 1 for yes,0 for no $pad_counts = 0; // 1 for yes,0 for no $hierarchical = 0; // 1 for yes,0 for no $title = ''; $empty = 0; $args = array( 'taxonomy' => $taxonomy,'orderby' => $orderby,'show_count' => $show_count,'pad_counts' => $pad_counts,'hierarchical' => $hierarchical,'title_li' => $title,'hide_empty' => $empty ); ?> <?PHP $all_categories = get_categories( $args ); //print_r($all_categories); foreach ($all_categories as $cat) { //print_r($cat); if($cat->category_parent == 0) { $category_id = $cat->term_id; ?> <?PHP echo '<br /><a href="'. get_term_link($cat->slug,'product_cat') .'">'. $cat->name .'</a>'; ?> <?PHP $args2 = array( 'taxonomy' => $taxonomy,'child_of' => 0,'parent' => $category_id,'hide_empty' => $empty ); $sub_cats = get_categories( $args2 ); if($sub_cats) { foreach($sub_cats as $sub_category) { echo $sub_category->name ; } } ?> <?PHP } } ?>
这工作正常,并返回产品类别列表.我一直在试图获得一个特定类别的产品列表.
示例:获取cat_id = 34的所有产品.
我知道产品存储为帖子,并且一直试图完成这项工作,但似乎不能.
如何获取特定类别的产品列表?
<?PHP $args = array( 'post_type' => 'product','posts_per_page' => 10,'product_cat' => 'hoodies' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>'; endwhile; wp_reset_query(); ?>