【问题标题】:Woocommerce Product child category from current parent category当前父类别中的 Woocommerce 产品子类别
【发布时间】:2016-06-04 13:48:30
【问题描述】:

我正在尝试显示相应父类别的 WooCommerce 产品子类别。我输入的产品如下:

我正在使用以下显示父类别列表的代码段:

<?php
$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'
   );

    $categories = get_categories($args);

    foreach ($categories as $category) {
        echo $category->name;
    }
?>

效果很好。现在我想修改我的代码段以显示如下输出:

Album
    English Songs
        Modern Songs
        Pop
        Rock

我该怎么做?

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    我已经解决了这个问题,在 woocommerce wordpress 中显示类别、子类别和产品如下,

    <?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
    );
    ?>
    
    <?php
    $all_categories = get_categories( $args );
    foreach ($all_categories as $cat)
    {
    
    if($cat->category_parent == 0)
    {
    $category_id = $cat->term_id;
    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
    $image = wp_get_attachment_url( $thumbnail_id );
    echo "<ul class='category'><li>".$cat->name;
    $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)
    {
    
    foreach($sub_cats as $sub_category)
    {
    echo "<ul class='subcategory'>";
    if($sub_cats->$sub_category == 0)
    {
    echo "<li>".$sub_category->cat_name;
    
    ?>
    <?php
    /*echo "<pre>";
    print_r($sub_category);
    echo "</pre>";*/
    
    $args = array( 'post_type' => 'product','product_cat' => $sub_category->slug);
    $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
    }
    else
    {
    echo "</li></ul></li>";
    }
    echo "</ul>";
    }
    }
    else
    {
    $args = array( 'post_type' => 'product', 'product_cat' => $cat->slug );
    $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();
    }
    }
    else
    {
    echo "</li></ul>";
    }
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      首先创建一个包含所有项目的数组

      $all = array(
              0 =>array(
                      "id"=> 1,
                      "name"=> "A",
                      "parent_id"=> 0
                  ),
               1 =>array(
                      "id"=> 2,
                      "name"=> "B",
                      "parent_id"=> 1
                  ),
               2 =>array(
                      "id"=> 3,
                      "name"=> "C",
                      "parent_id"=> 1
                  ),
               3 =>array(
                      "id"=> 4,
                      "name"=> "D",
                      "parent_id"=> 2
                  ),
              4 =>array(
                      "id"=> 5,
                      "name"=> "E",
                      "parent_id"=> 0
                  ),
          );      
      

      使用此函数从您的项目中创建递归 n 次数组

      function makeMenu($items, $parentId)
      {
          $menu = array_filter($items, function ($item) use ($parentId) {
              return     $item['parent_id'] == $parentId;
          });
          foreach ($menu as &$item) 
          {
              $subItems = makeMenu($items, $item['id']);
              if (!empty($subItems)) 
              {
                  $item['sub_menu'] = $subItems;
              }
          }
          return $menu;
      }
      

      调用函数makeMenu($yourArray,$parentID)

      makeMenu($all,0); ////parent ID is 0 by default while calling..
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-11
        • 2017-06-04
        • 2021-09-03
        相关资源
        最近更新 更多