2014-09-23 3 views
3

저는 자동차 부품을 판매하는 Wordpress WooCommerce 사이트가 있습니다. 각 파트 (제품)에 대해 파트에 지정할 수있는 고유 한 제품 카테고리를 만들었습니다. 그래서 예. 헤드 라이트 (부품)는 3-Door 1999 Blue Alfa Romeo 156 1.1 가솔린 매뉴얼에서 얻을 수 있습니다.Wordpress WooCommerce는 개별 제품에 대해 중첩 된 제품 카테고리를 표시합니다.

개별 제품 페이지에서이 부품과 관련된 제품 범주 만 중첩 목록을 표시하려고합니다. 따라서 파트에 태그를 지정하면 아래 이미지와 같은 중첩 된 뷰가 표시됩니다. enter image description here

그러나 두 번째 이미지 아래의 현재 코드에는이 부분을 포함하여 연결된 부품이있는 모든 제품 카테고리가 표시됩니다. 아래의 두 번째 이미지에서 볼 수 있듯이 나는 다른 자동차 제작자에게 할당 된 많은 부품을 보유하고 있으며이 부품을 위해 모두 표시됩니다. 나는이 부분이이 부분과 관련된 Product Categories를 표시하기를 원합니다. 따라서 Alfa Romeo 만 표시해야합니다.이 부분에 태그가 지정되었는지 여부에 관계없이 부품이 포함 된 다른 모든 제품 카테고리가 아닙니다. 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

의 사용
관련 문제