如何在WooCommerce中仅显示子分类及对应商品?
如何在WooCommerce中仅显示子分类及对应商品?
现有代码会显示全站所有分类及对应商品(如有),但我需要仅显示子分类,而非全站所有分类,已经为此困扰两天了。
现有显示全部分类及商品的代码:
<?php $taxonomy = 'product_cat'; $orderby = 'name'; $order = 'ASC'; $show_count = 0; // 1 for yes, 0 for no $pad_counts = 0; // 1 for yes, 0 for no $hierarchical = 1; // 1 for yes, 0 for no $title = ''; $empty = 1; $args = array( 'taxonomy' => $taxonomy, 'orderby' => $orderby, 'order' => $order, 'show_count' => $show_count, 'pad_counts' => $pad_counts, 'hierarchical' => $hierarchical, 'title_li' => $title, 'hide_empty' => $empty, 'parent' => 0 ); $all_categories = get_categories( $args ); foreach ($all_categories as $cat) { $category_id = $cat->term_id; $args2 = array('taxonomy' => $taxonomy,'parent' => $category_id,'hierarchical' => $hierarchical, 'orderby' => $orderby, 'order' => $order,'hide_empty' => $empty); $categories = get_categories( $args2 ); $categories_cnt = count(get_categories( $args2 )); if ($categories_cnt != 0){ echo "<ul class='category'><li>".$cat->name; $sub_cats = get_categories( $args2 ); if($sub_cats) { echo "<ul>"; foreach($sub_cats as $sub_category) { echo "<li>".$sub_category->cat_name; $args = array( 'post_type' => 'product','product_cat' => $sub_category->slug, 'orderby' => $orderby, 'order' => $order); $loop = new WP_Query( $args ); echo "<ul class='products'>"; while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li> <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>"> <?php the_title(); ?> </a></li> <?php endwhile; ?> </ul> <?php wp_reset_query(); ?> <?php echo "</li>"; } echo "</ul></ul>"; } } else { echo "<ul class='category'><li>".$cat->name; $args = array( 'post_type' => 'product', 'product_cat' => $cat->slug, 'orderby' => $orderby, 'order' => $order ); $loop = new WP_Query( $args ); echo "<ul class='products'>"; while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li> <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>"> <?php the_title(); ?> </a></li> <?php endwhile; ?> </ul></li></ul> <?php wp_reset_query(); echo "</li></ul>"; } } ?>
可正常输出子分类列表但无法显示商品的代码:
<?php if ( is_product_category() ) { $term = get_queried_object(); $taxonomy = $term->taxonomy; echo '<h3>Запчасти для ' . $term->name . '<h3>'; // выводим текущую категорию echo '<div class="subcategories-woc">'; // получаем дочерние, если существуют if ( $term_children = get_term_children( $term->term_id, $taxonomy ) ) { echo '<ul>'; foreach ( $term_children as $key => $term_child ) { $term_child = get_term_by( 'id', $term_child, $taxonomy ); // выводим дочерние if ( $term->term_id == $term_child->parent ) { echo '<li class="shadow"><a href="' . get_term_link( $term_child->term_id, $taxonomy ) . '">' . $term_child->name . '</a></li>'; } } echo '</ul>'; } echo '</div>'; } ?>
内容的提问来源于stack exchange,提问作者Deniskapipiska
相关产品推荐
相关产品推荐

