問題描述

我有一個分層的自定義分類法,我可以使用 print_r(get_the_terms( $post->ID, 'taxonomic_rank' )); 顯示:

Array
(
    [46] => stdClass Object
        (
            [term_id] => 46
            [name] => Aplocheilidae
            [slug] => aplocheilidae
            [term_group] => 0
            [term_taxonomy_id] => 53
            [taxonomy] => taxonomic_ranks
            [description] =>
            [parent] => 39
            [count] => 1
            [object_id] => 443
        )

    [47] => stdClass Object
        (
            [term_id] => 47
            [name] => Aplocheilus
            [slug] => aplocheilus
            [term_group] => 0
            [term_taxonomy_id] => 54
            [taxonomy] => taxonomic_ranks
            [description] =>
            [parent] => 46
            [count] => 1
            [object_id] => 443
        )

    [39] => stdClass Object
        (
            [term_id] => 39
            [name] => Cyprinodontiformes
            [slug] => cyprinodontiformes
            [term_group] => 0
            [term_taxonomy_id] => 52
            [taxonomy] => taxonomic_ranks
            [description] =>
            [parent] => 0
            [count] => 1
            [object_id] => 443
        )

)

此分類法將始終採用以下形式:Order(parent)> 家庭 (訂單的孩子)> Sub-family(家庭的孩子)

是否有一個快捷方便的方式以正確的順序顯示這些分類,以便打印出以下內容? Order: <order>, Family: <family>, Sub-family: <sub-family>

提前致謝

最佳解決方案

可能有更好的方法可以做到這一點,但總是可以做三個簡單的 foreach 循環。

我寫了一個功能很好的示例功能,應該為您服務是一個很好的起點:

function print_taxonomic_ranks( $terms = '' ){

    // check input
    if ( empty( $terms ) || is_wp_error( $terms ) || ! is_array( $terms ) )
        return;

    // set id variables to 0 for easy check
    $order_id = $family_id = $subfamily_id = 0;

    // get order
    foreach ( $terms as $term ) {
        if ( $order_id || $term->parent )
            continue;
        $order_id  = $term->term_id;
        $order     = $term->name;
    }

    // get family
    foreach ( $terms as $term ) {
        if ( $family_id || $order_id != $term->parent )
            continue;
        $family_id = $term->term_id;
        $family    = $term->name;
    }

    // get subfamily
    foreach ( $terms as $term ) {
        if ( $subfamily_id || $family_id != $term->parent )
            continue;
        $subfamily_id = $term->term_id;
        $subfamily    = $term->name;
    }

    // output
    echo "Order: $order, Family: $family, Sub-family: $subfamily";

}

讓它居住在您的 functions.php 文件中,並在您的模板中使用它:

print_taxonomy_ranks( get_the_terms( $post->ID, 'taxonomic_rank' ) );

注意:循環相同的陣列三次聽起來有點愚蠢,但另一方面它是一個快速和容易的解決方案,易於閲讀,擴展和維護。

次佳解決方案

雖然 Maugly 的方法看起來更可讀,但在陣列上運行了 3 次循環似乎對我來説是不對的。所以這裏只是另一種方法,對於一些但是沒有運行循環 3 次的工作而言,它們的讀取可能性較差。

function print_taxonomy_ranks( $terms ) {
    // if terms is not array or its empty don't proceed
    if ( ! is_array( $terms ) || empty( $terms ) ) {
        return false;
    }

    foreach ( $terms as $term ) {
        // if the term have a parent, set the child term as attribute in parent term
        if ( $term->parent != 0 )  {
            $terms[$term->parent]->child = $term;
        } else {
            // record the parent term
            $parent = $term;
        }
    }

    echo "Order: $parent->name, Family: {$parent->child->name}, Sub-Family: {$parent->child->child->name}";
}

第三種解決方案

我有一種情況,一個帖子可以被標記為多個類別組,以及父類別中的多個孩子,所以我希望我的層次結構能反映出來。我也只是想要幾行代碼:

$terms = get_the_terms($id, 'department_categories');
foreach($terms as $key => $term){
    if($term->parent != 0){
        $terms[$term->parent]->children[] = $term;
        unset($terms[$key]);
    }
}

基本上,它找到一個類別的父代,它將它作為一個孩子移動到父的 obj,然後將其從數組中的原始位置刪除。我已經使用多個兄弟姐妹,孩子和不同級別的類別進行了測試。

希望有人發現這是有用的,以防他們只是尋找一些邏輯指導而不是”plugin”!

參考文獻

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