简体   繁体   中英

Woocommerce sub category loop

I am working on a listing of subcategories for Woocommerce and I am stuck,

The code for the product loop

    <?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(); ?>

But I can not find a decent way to simply get a listing of the sub-product-categories instead of the products.

Thanks in advance

By 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>';
 }

By Name:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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