2017-04-25 23 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; 
} 
関連する問題