Woocommerce子类别循环

时间:2015-06-11 13:13:50

标签: php wordpress loops woocommerce

我正在制作Woocommerce的子类别列表,我被困住了,

产品循环的代码

    <?php
$args = array ( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 12, 'product_cat' => '2', 'orderby' => 'date','order' => 'DESC' );

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

                        <li>    

                             <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><h3 ><?php the_title(); ?></h3></a>

                        </li>
            <?php endwhile; ?>
            <?php wp_reset_query(); ?>

但我找不到一个合适的方法来简单地获得子产品类别而不是产品的列表。

提前致谢

1 个答案:

答案 0 :(得分:1)

按ID:

function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
$args = array(
   'hierarchical' => 1,
   'show_option_none' => '',
   'hide_empty' => 0,
   'parent' => $parent_cat_ID,
   'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul class="wooc_sclist">';
  foreach ($subcats as $sc) {
    $link = get_term_link( $sc->slug, $sc->taxonomy );
      echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
  }
  echo '</ul>';
 }

按姓名:

function woocommerce_subcats_from_parentcat_by_NAME($parent_cat_NAME) {
$IDbyNAME = get_term_by('name', $parent_cat_NAME, 'product_cat');
$product_cat_ID = $IDbyNAME->term_id;
$args = array(
   'hierarchical' => 1,
   'show_option_none' => '',
   'hide_empty' => 0,
   'parent' => $product_cat_ID,
   'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul class="wooc_sclist">';
  foreach ($subcats as $sc) {
    $link = get_term_link( $sc->slug, $sc->taxonomy );
      echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
  }
echo '</ul>';
}

Source / Inspiration