php
  • wordpress
  • woocommerce
  • 2017-04-18 68 views 0 likes 
    0

    我想在前端的所有woocommerce類的,像這樣的結果子類別:得到woocommerce類與子類

    <ul> 
        <li><a href="">Link</a> 
         <ul> 
          <li><a href="">Submenu link</a></li> 
         </ul> 
        </li> 
    </ul> 
    

    這裏是我有(但它不是我想要的):

    <?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;  
         echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>'; 
    
         $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 '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>'; 
           } 
          } 
         }  
    } 
    ?> 
    

    這段代碼顯示的類別和子類別,但子類不應該放在這裏,子類是像這樣的單獨鏈接:

    <ul> 
        <li><a href="">link</a></li> 
        <li><a href="">Submenu link</a></li> 
    </ul> 
    
    +3

    您的代碼複製從這裏粘貼:http://stackoverflow.com/questions/21009516/get-categories-from-wordpress-woocommerce你嘗試過什麼到目前爲止,或者你在哪裏遇到問題? –

    +0

    檢查了這一點 - http://rachievee.com/wp-tutorial-how-to-create-a-categories-and-sub-categories-menu-part-1/ –

    +0

    也檢查一次 - http:// www .wpworking.com/hacks-2/new-hack-for-displays-a-category-subcategory-menu-on-word/ –

    回答

    2

    你可以試試這個代碼:

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

    這將在您的WooCommerce,基於WordPress站點打印。

    0
    <?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;  
         echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; 
    
         $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 $sub_category->name ; 
          } 
         } 
        }  
    } 
    ?> 
    
    +0

    你應該解釋一下你的代碼。 – Benjamin

    +0

    你好本傑明我做了兩個查詢一個提取類別和一個提取子類別,也使用自定義分類,使郵政類別不會混入類別的woocommerce產品 我們已經取得第二個查詢取得子類別根據父類別和使用foreach我們可以顯示我們想要顯示的類別和子類別 – KTrivedi

    +0

    我的意思是,在你的問題的主體:)你可以使用編輯鏈接。這將有助於OP瞭解它! – Benjamin

    相關問題