WooCommerce子类别帮助

时间:2018-09-25 作者:Jenova1628

New Uploaded Code:

<div class="col-sm-3 col-sm-pull-9 sidebar">
                    <div class="list-group products box">
                        <h4>Product Range</h4>
                        <?php $terms = get_the_terms( $post->ID, \'product_cat\' );
                        foreach ( $terms as $term ){
                        $category_name = $term->name;
                        $parent_category_id = $term->term_id;

                        $categories=get_categories(array( \'parent\' => $category_id ));
                    $children = get_terms( \'product_cat\', array(
                                \'parent\'    => $parent_category_id,
                                \'hide_empty\' => false
                    ) );
                  foreach( $children as $subcat ){
                        ?>
                        <a href="<?php get_term_link( $subcat->slug, \'product_cat\' ); ?>" class="list-group-item"><?php echo $subcat->name; ?></a>
                        <?php } } ?>
                </div>
2 个回复
最合适的回答,由SO网友:Pratik Patel 整理而成
    <div class="col-sm-3 col-sm-pull-9 sidebar">
                        <div class="list-group products box">
                            <h4>Product Range</h4>
                            <?php $terms = get_the_terms( $post->ID, \'product_cat\' );
                            foreach ( $terms as $term ){
                            $category_name = $term->name;
                            $parent_category_id = $term->term_id;

                            $categories=get_categories(array( \'parent\' => $category_id ));
                        $children = get_terms( \'product_cat\', array(
                                    \'parent\'    => $parent_category_id,
                                    \'hide_empty\' => false
                        ) );
                      foreach( $children as $subcat ){
                            ?>

                            <a href="<?php echo get_category_link($subcat->term_id, \'product_cat\'); ?>" class="list-group-item"><?php echo $subcat->name; ?></a>
                            <?php } } ?>
                    </div>
SO网友:Praveen

Try this:

    $args = array(
      \'taxonomy\' => \'product_cat\',
      \'hide_empty\' => false,
      \'parent\'   => 0
  );
$product_cat = get_terms( $args );



  foreach ($product_cat as $parent_product_cat)
  {

  echo \'
      <ul>
        <li><a href="\'.get_term_link($parent_product_cat->term_id).\'">\'.$parent_product_cat->name.\'</a>
        <ul>
          \';
  $child_args = array(
              \'taxonomy\' => \'product_cat\',
              \'hide_empty\' => false,
              \'parent\'   => $parent_product_cat->term_id
          );
  $child_product_cats = get_terms( $child_args );
  foreach ($child_product_cats as $child_product_cat)
  {
    echo \'<li><a href="\'.get_term_link($child_product_cat->term_id).\'">\'.$child_product_cat->name.\'</a></li>\';
  }

  echo \'</ul>
      </li>
    </ul>\';
  }
结束