問題描述

如何獲得給定期限的 top-level 家長?

我正在使用 wp_get_object_terms 在帖子上獲取分類術語,但不是顯示所有標記的條款,我只想顯示標記詞語 「top-level」 父母。

所以如果這些是我選擇的條款,我只想展示早餐,午餐和晚餐。

x BREAKFAST
   x Cereal
   x Eggs
  LUNCH
     Hamburger
   x Pizza
  DINNER
     Fish
        Bass
      x Salmon
        Trout
     Lasagna

我該怎麼做?

最佳解決方案

感謝 Ivaylo 的程式碼,這是基於 Bainternet 的答案。

第一個函式 (get_term_top_most_parent) 接受術語 ID 和分類,並返回該術語的 top-level 父項 (或術語本身,如果是無父母); 第二個函式 (hey_top_parents) 在迴圈中工作,並且給定一個分類法,返回一個帖子條款的 top-level 祖先。

// 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;
}

// so once you have this function you can just loop over the results returned by wp_get_object_terms

function hey_top_parents($taxonomy, $results = 1) {
    // get terms for current post
    $terms = wp_get_object_terms( get_the_ID(), $taxonomy );
    // set vars
    $top_parent_terms = array();
    foreach ( $terms as $term ) {
        //get top level parent
        $top_parent = get_term_top_most_parent( $term->term_id, $taxonomy );
        //check if you have it in your array to only add it once
        if ( !in_array( $top_parent, $top_parent_terms ) ) {
            $top_parent_terms[] = $top_parent;
        }
    }
    // build output (the HTML is up to you)


    foreach ( $top_parent_terms as $term ) {

          $r = '<ul>';
          $r .= '<li><a href="http://'.%20get_term_link(%20$term->slug,%20$taxonomy%20)%20.%20'">' . $term->name . '</a></li>';
    }
          $r .= '</ul>';

    // return the results
    return $r;

}

次佳解決方案

自 3.1.0 起,get_ancestors()可用。它在層次結構中返回一個從最低到最高的祖先陣列。

第三種解決方案

這是一個簡單的功能,可以讓您獲得任何特定術語的最高階別的術語:

function get_term_top_most_parent( $term_id, $taxonomy ) {
    $parent  = get_term_by( 'id', $term_id, $taxonomy );
    while ( $parent->parent != 0 ){
        $parent  = get_term_by( 'id', $parent->parent, $taxonomy );
    }
    return $parent;
}

一旦你有這個功能,你可以迴圈 wp_get_object_terms 返回的結果:

$terms =  wp_get_object_terms( $post->ID, 'taxonomy' );
$top_parent_terms = array();
foreach ( $terms as $term ) {

    //Get top level parent
    $top_parent = get_term_top_most_parent( $term->ID, 'taxomony' );

    //Check if you have it in your array to only add it once
    if ( !in_array( $top_parent->ID, $top_parent_terms ) ) {
        $top_parent_terms[] = $top_parent;
    }
}

第四種方案

/**
 * Get top level term
 */
function get_top_level_term($term,$taxonomy){
    if($term->parent==0) return $term;
    $parent = get_term( $term->parent,$taxonomy);
    return get_top_level_term( $parent , $taxonomy );
}

第五種方案

我有同樣的問題,我很容易解決。看一下這個:

定義 $taxonomy 。它可以是您要獲取資料的分類法。這樣做後,你可以簡單地做:

<?php
    $postterms = wp_get_post_terms($post->ID, $taxonomy);   // get post terms
    $parentId = $postterms[0]->parent;                      // get parent term ID
    $parentObj = get_term_by('id', $parentId, $taxonomy);   // get parent object 
?>

現在你有這樣的東西:

object(stdClass)#98 (11) {
  ["term_id"]=>
  int(3)
  ["name"]=>
  string(8) "Esportes"
  ["slug"]=>
  string(8) "esportes"
  ["term_group"]=>
  int(0)
  ["term_taxonomy_id"]=>
  int(3)
  ["taxonomy"]=>
  string(17) "noticiaseditorias"
  ["description"]=>
  string(0) ""
  ["parent"]=>
  int(0)
  ["count"]=>
  int(4)
  ["object_id"]=>
  int(123)
  ["filter"]=>
  string(3) "raw"
}

你可以使用 $parentObj 來獲取 slug,name,id 等等。只需使用 $parentObj->slug$parentObj->name 作為例子。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。