問題描述

我如何獲得一個職位類別的分類?

如果我有一個帖子類型 event,我需要找出附加到該帖子類型的分類列表。如何找到它們?

最佳解決方案

嘿,我想我得到了!在 WordPress 中的 taxonomy.php 文件中查看幾個功能後,我發現這個功能 get_object_taxonomies(); 做了這個技巧:)

這是功能

function get_post_taxonomies($post) {
    // Passing an object
    // Why another var?? $output = 'objects'; // name / objects
    $taxonomies = get_object_taxonomies($post, 'objects');

    /*// Passing a string using get_post_type: return (string) post, page, custom...
    $post_type  = get_post_type($post);
    $taxonomies = get_object_taxonomies($post_type, 'objects');*/

    /*// In the loop with the ID
    $theID      = get_the_ID();
    $post_type  = get_post_type($theID);
    $taxonomies = get_object_taxonomies($post_type, 'objects');*/

    // You can also use the global $post

    // edited to fix previous error $taxonomies
    // edited to force type hinting array
    return (array) $taxonomies; // returning array of taxonomies
}

次佳解決方案

 get_categories 會做這個工作。

get_categories('taxonomy=taxonomy_name&type=custom_post_type'); 

參考文獻

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