2017-04-25 1 views
0

나는 계층 적 사용자 정의 세금 (woocom 제품)을 보유하고 있으며 일부 빵 부스러기를 빌드하려고합니다. 나는 get_ancestors()을 발견했고, 용어를 사용하려고 생각하고있다. 그러나 내가 제품에 도착하면 나는 길을 잃는다. 그것이 속한 즉각적인 최상위 범주 (즉, 제품 목록)를 얻는 방법? 여기 WooCommerce 제품의 상위 부모 카테고리를 가져 오는 방법은 무엇입니까?

All Categories **term ID 192** 
-Sub Cat One **term id 204** 
-- Sub Cat Two (products listed out here) **term id 207** 
--- Product 

당신은 탐색 경로를 구축하기 위해이 코드를 사용할 수 있습니다

function build_breadcrumbs($product_cat_id, $type) { 

    if('product' === $type) { 
     $terms = get_the_terms($product_cat_id, 'product_cat'); 
     error_log(print_r( $terms, true)); 
    } 
    else { 
     $ancestors = get_ancestors($product_cat_id, $type); 
     error_log(print_r( $ancestors, true)); 
    } 

} 

답변

0

, 그냥 내가 함께이 문제를 알아 내려고 노력 넣어 코드입니다 지금은 결과를 기록합니다. 이렇게하면 계층 적 순서로 모든 제품 범주가 나열됩니다.

function cd_get_term_parents($id, $taxonomy, $link = false, $separator = ' >> ', $nicename = false, $visited = array()) { 
    $chain = ''; 
    $parent = &get_term($id, $taxonomy); 
    if (is_wp_error($parent)) 
      return $parent; 

    if ($nicename) { 
      $name = $parent->slug; 
    } else { 
      $name = $parent->name; 
    } 

    if ($parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited)) { 
      $visited[] = $parent->parent; 
      $chain .= cd_get_term_parents($parent->parent, $taxonomy, $link, $separator, $nicename, $visited); 
    } 

    if ($link) { 
      $chain .= '<a href="' . get_term_link($parent, $taxonomy) . '" title="' . esc_attr(sprintf(_e("View all posts in %s"), $parent->name)) . '">'.$parent->name.'</a>' . $separator; 
    } else { 
      $chain .= $name.$separator; 
    } 
    return $chain; 
} 
관련 문제