2016-07-12 6 views
0

get_category_parents() のドキュメントを見ると、カテゴリの名前がセパレータによって区切られた文字列が返されることがわかります。今、私はこれが私のものかどうか分かりませんが、これは非常にばかげているようです。私が期待しているのは、実際のカテゴリオブジェクトの配列です。私が推測することはすべてできません。Wordpressはカテゴリの配列としてカテゴリの親を取得します

親カテゴリを自分が望む形式で取得するにはどうすればよいでしょうか?

$categories = []; 
$parents = get_category_parents($cat, false, ','); 
foreach($parents as $parent){ 
    $categories[] = get_term_by('name', $parent, 'category'); 
} 

テストされていない:

答えて

1

は、あなたの希望は、ポストの親カテゴリを取得する場合は、正しい結果

を取得します、このいずれかを試してみてくださいあなたは、カテゴリIDを持つ親を取得したい場合は、この

$parent_cat_array = get_post_ancestors($post); //$post is an object or you can pass post id also 

を試し、その後これを試してみてください。.. functions.php

呼び出しでこのfunctioを配置する。

// determine the topmost parent of a term 
    function get_term_top_most_parent($term_id, $taxonomy) { 
     // start from the current term 
     $parent = get_term_by('id', $term_id, $taxonomy); 
     // climb up the hierarchy until we reach a term with parent = '0' 
     while ($parent->parent != '0') { 
     $term_id = $parent->parent; 

     $parent = get_term_by('id', $term_id, $taxonomy); 
     } 
     return $parent; 
    } 

機能nはここで

 $term_parent = get_term_top_most_parent($term_id, $taxonomy); 
     // in case of default category then your taxonomy is category 

あなたのページ、ポストまたはカスタムテンプレートでは、あなたの最も親カテゴリまたはカスタム分類 検査済みを返す関数であります!

1

はこのような何かを試してみてください。

関連する問題