如何在woocommerce wordpress中显示子子类别

时间:2017-07-14 09:17:47

标签: wordpress woocommerce categories

我正在尝试获取子类别子类别的产品类别

<?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 = 1;      // 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
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
  if($cat->category_parent == 0) {

      $category_id = $cat->term_id;

       $args2 = array(
              'taxonomy'     => $taxonomy,
              'child_of'     => 0,
              'parent'       => $category_id,
              'orderby'      => $orderby,
              'show_count'   => $show_count,
              'pad_counts'   => $pad_counts,
              'hierarchical' => $hierarchical,
              'title_li'     => $title,
              'hide_empty'   => $empty
      );

    $sub_cats = get_categories( $args2 );

    if( $sub_cats ) {
      echo '<li class="title '.$cat->name.'"><a >
          '; 
      echo $cat->name ;
      echo '<i class="chevron right icon"></i>
        </a></li>';
      $sub_cats = "";
    }else {
      echo '<li class="titleVide '.$cat->name.'Hide"><a >
          '; 
      echo $cat->name ;
      echo '</a></li>';
      $sub_cats = "";
    }


      $args2 = array(
              'taxonomy'     => $taxonomy,
              'child_of'     => 0,
              'parent'       => $category_id,
              'orderby'      => $orderby,
              'show_count'   => $show_count,
              'pad_counts'   => $pad_counts,
              'hierarchical' => $hierarchical,
              'title_li'     => $title,
              'hide_empty'   => $empty
      );
      $sub_cats = get_categories( $args2 );
      if($sub_cats) {

        echo '<li class="content"> <p>';
          foreach($sub_cats as $sub_category) {

            echo '
                <a href="'. get_term_link($sub_category->slug, "product_cat" ) .'">'. $sub_category->name . '</a>
             ';


          }

          echo ' </p></li>';
      } else {
    echo '';
      }

      $sub_cats = "";
  }       
} /* end foreach all_categories cat */

wp_reset_query();
?>

此代码按层次列出了它们下面的所有顶级类别和子类别,但我有子类别(子子类别)的子类别,所以我如何列出这些子子类别(带有clic)。

Categories IN BO

List categories in FO

1 个答案:

答案 0 :(得分:0)

使用child_of =当前类别ID

尝试以下代码
<?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 = 1;      // 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
    );
    $all_categories = get_categories( $args );
    foreach ($all_categories as $cat) {
      if($cat->category_parent == 0) {

          $category_id = $cat->term_id;

           $args2 = array(
                  'taxonomy'     => $taxonomy,
                  'child_of'     => 0,
                  'parent'       => $category_id,
                  'orderby'      => $orderby,
                  'show_count'   => $show_count,
                  'pad_counts'   => $pad_counts,
                  'hierarchical' => $hierarchical,
                  'title_li'     => $title,
                  'hide_empty'   => $empty
          );

        $sub_cats = get_categories( $args2 );

        if( $sub_cats ) {
          echo '<li class="title '.$cat->name.'"><a >
              '; 
          echo $cat->name ;
          echo '<i class="chevron right icon"></i>
            </a></li>';
          $sub_cats = "";
        }else {
          echo '<li class="titleVide '.$cat->name.'Hide"><a >
              '; 
          echo $cat->name ;
          echo '</a></li>';
          $sub_cats = "";
        }


          $args2 = array(
                  'taxonomy'     => $taxonomy,
                  'child_of'     => $category_id,
                  'orderby'      => $orderby,
                  'show_count'   => $show_count,
                  'pad_counts'   => $pad_counts,
                  'hierarchical' => $hierarchical,
                  'title_li'     => $title,
                  'hide_empty'   => $empty
          );
          $sub_cats = get_categories( $args2 );
          if($sub_cats) {

            echo '<li class="content"> <p>';
              foreach($sub_cats as $sub_category) {

                echo '
                    <a href="'. get_term_link($sub_category->slug, "product_cat" ) .'">'. $sub_category->name . '</a>
                 ';


              }

              echo ' </p></li>';
          } else {
        echo '';
          }

          $sub_cats = "";
      }       
    } /* end foreach all_categories cat */

    wp_reset_query();

?>

或使用wp_list_categories()函数列出类别 https://developer.wordpress.org/reference/functions/wp_list_categories/