2014-09-23 201 views
3

我有一個WordPress的WooCommerce網站,銷售汽車零部件。對於每個零件(產品),我創建了可分配給零件的獨特產品類別。所以例如前大燈(部分)可以來自3門1999藍色阿爾法羅密歐156 1.1汽油手冊。WordPress的WooCommerce顯示個別產品的嵌套產品類別

在單個產品頁面上,我想顯示僅與本部分關聯的產品類別的嵌套列表。所以當我標記一個部分時,我會有一個嵌套的視圖,如下圖所示。 enter image description here

但是,我目前的代碼顯示在第二張圖片下方,顯示了所有與其相關的產品類別,包括這部分。從下面的第二張圖中可以看出,我有許多其他零件分配給其他汽車製造商,並且它們都顯示爲這部分。我只希望本部分顯示與本部分相關的產品類別。因此,在創建它應該只顯示阿爾法羅密歐 - 而不是所有其他產品類別,其中有部分內容,無論他們在這部分標記。 enter image description here

任何人都可以請幫忙嗎?

當前代碼

<?php 
    $woocCategoryTerms = get_terms('product_cat', array(
     'order'  => 'ASC', 
     'hide_empty' => true, // (boolean) 
     'parent'  => 0,  // (integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string. 
     'hierarchical' => true, // (boolean) Whether to include terms that have non-empty descendants (even if 'hide_empty' is set to true). 
     ));  

    foreach($woocCategoryTerms as $wooCategoryTerm) : 
?> 
     <ul> 
      <li> 
       <a href="<?php echo get_term_link($wooCategoryTerm -> slug, $wooCategoryTerm -> taxonomy); ?>"> 
        <?php 
         echo $wooCategoryTerm -> name; 
        ?> 
       </a> 
       <ul class="wsubcategs"> 
        <?php 
         $wooSubArgs = array(
          'hierarchical' => true, 
          'hide_empty' => true, 
          'parent' => $wooCategoryTerm -> term_id, 
          'taxonomy' => 'product_cat' 
         ); 

         $wooSubCategories = get_categories($wooSubArgs); 

         foreach ($wooSubCategories as $wooSubCategory): 
        ?> 
          <li> 
           <a href="<?php echo get_term_link($wooSubCategory -> slug, $wooSubCategory -> taxonomy);?>"> 
            <?php 
             echo $wooSubCategory -> name; 
            ?> 
           </a> 
          </li> 
          <?php 
         endforeach; 
          ?> 
       </ul> 
      </li> 
     </ul> 
     <?php 
    endforeach; 
     ?> 

回答

4

get_terms回報給出了具體的分類,而不是帖子的全部條款。您在這裏有幾個選擇,但我喜歡使用wp_list_categories的靈活性。它不僅與建設工程類別,但也與自定義分類

下面是從食品爲例

<?php 
$taxonomy = 'category'; //change to your taxonomy name 

// get the term IDs assigned to post. 
$post_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids')); 
// separator between links 
$separator = ', '; 

if ( !empty($post_terms) && !is_wp_error($post_terms)) { 

    $term_ids = implode(',' , $post_terms); 
$terms = wp_list_categories('title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids); 
$terms = rtrim(trim( str_replace('<br />', $separator, $terms)), $separator); 

// display post categories 
echo $terms; 
} 
?> 

您也可以使用get_the_terms

相關問題