[WordPress] 透過 get_terms() 取得所有標籤、分類...的 id、內容...等

在現在的 WordPress 要取得網站所有標籤、分類...的 id、內容,要使用 get_terms() 並透過傳入參數取得對應的內容。

使用方法簡單如下:

$terms = get_terms( array(
    'taxonomy'   => 'post_tag'
) );

taxonomy 基本上是一定要傳的,傳入 post_tag 會抓到標籤,要抓分類的話則是傳入 category。

完整可以傳入的參數如下:

 

  • taxonomy string|string[]
    Taxonomy name, or array of taxonomy names, to which results should be limited.
  • object_ids int|int[]
    Object ID, or array of object IDs. Results will be limited to terms associated with these objects.
  • orderby string
    Field(s) to order terms by. Accepts:
    • Term fields ('name', 'slug', 'term_group', 'term_id', 'id', 'description', 'parent', 'term_order'). Unless $object_ids is not empty, 'term_order' is treated the same as 'term_id'.
    • 'count' to use the number of objects associated with the term.
    • 'include' to match the 'order' of the $include param.
    • 'slug__in' to match the 'order' of the $slug param.
    • 'meta_value'
    • 'meta_value_num'.
    • The value of $meta_key.
    • The array keys of $meta_query.
    • 'none' to omit the ORDER BY clause.
    Default 'name'.
  • order string
    Whether to order terms in ascending or descending order.
    Accepts 'ASC' (ascending) or 'DESC' (descending).
    Default 'ASC'.
  • hide_empty bool|int
    Whether to hide terms not assigned to any posts. Accepts 1|true or 0|false. Default 1|true.
  • include int[]|string
    Array or comma/space-separated string of term IDs to include.
    Default empty array.
  • exclude int[]|string
    Array or comma/space-separated string of term IDs to exclude.
    If $include is non-empty, $exclude is ignored.
    Default empty array.
  • exclude_tree int[]|string
    Array or comma/space-separated string of term IDs to exclude along with all of their descendant terms. If $include is non-empty, $exclude_tree is ignored. Default empty array.
  • number int|string
    Maximum number of terms to return. Accepts ''|0 (all) or any positive number. Default ''|0 (all). Note that $number may not return accurate results when coupled with $object_ids.
    See #41796 for details.
  • offset int
    The number by which to offset the terms query.
  • fields string
    Term fields to query for. Accepts:
    • 'all' Returns an array of complete term objects (WP_Term[]).
    • 'all_with_object_id' Returns an array of term objects with the 'object_id' param (WP_Term[]). Works only when the $object_ids parameter is populated.
    • 'ids' Returns an array of term IDs (int[]).
    • 'tt_ids' Returns an array of term taxonomy IDs (int[]).
    • 'names' Returns an array of term names (string[]).
    • 'slugs' Returns an array of term slugs (string[]).
    • 'count' Returns the number of matching terms (int).
    • 'id=>parent' Returns an associative array of parent term IDs, keyed by term ID (int[]).
    • 'id=>name' Returns an associative array of term names, keyed by term ID (string[]).
    • 'id=>slug' Returns an associative array of term slugs, keyed by term ID (string[]).
    Default 'all'.
  • count bool
    Whether to return a term count. If true, will take precedence over $fields. Default false.
  • name string|string[]
    Name or array of names to return term(s) for.
  • slug string|string[]
    Slug or array of slugs to return term(s) for.
  • term_taxonomy_id int|int[]
    Term taxonomy ID, or array of term taxonomy IDs, to match when querying terms.
  • hierarchical bool
    Whether to include terms that have non-empty descendants (even if $hide_empty is set to true). Default true.
  • search string
    Search criteria to match terms. Will be SQL-formatted with wildcards before and after.
  • name__like string
    Retrieve terms with criteria by which a term is LIKE $name__like.
  • description__like string
    Retrieve terms where the description is LIKE $description__like.
  • pad_counts bool
    Whether to pad the quantity of a term's children in the quantity of each term's "count" object variable. Default false.
  • get string
    Whether to return terms regardless of ancestry or whether the terms are empty. Accepts 'all' or '' (disabled). Default ''.
  • child_of int
    Term ID to retrieve child terms of. If multiple taxonomies are passed, $child_of is ignored. Default 0.
  • parent int
    Parent term ID to retrieve direct-child terms of.
  • childless bool
    True to limit results to terms that have no children.
    This parameter has no effect on non-hierarchical taxonomies.
    Default false.
  • cache_domain string
    Unique cache key to be produced when this query is stored in an object cache. Default 'core'.
  • update_term_meta_cache bool
    Whether to prime meta caches for matched terms. Default true.
  • meta_key string|string[]
    Meta key or keys to filter by.
  • meta_value string|string[]
    Meta value or values to filter by.
  • meta_compare string
    MySQL operator used for comparing the meta value.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_compare_key string
    MySQL operator used for comparing the meta key.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_type string
    MySQL data type that the meta_value column will be CAST to for comparisons.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_type_key string
    MySQL data type that the meta_key column will be CAST to for comparisons.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_query array
    An associative array of WP_Meta_Query arguments.
    See WP_Meta_Query::__construct() for accepted values.

 

回傳後便可從 term_id 取得 id 以及 name 取得名稱。

完整的 get_terms() 說明

 

課程推薦

AI工作術全面學習實戰營:6 堂精選課程,學會最好用 AI 工具,翻轉你的人生

AI工作術全面學習實戰營:6 堂精選課程,學會最好用 AI 工具,翻轉你的人生

《PChome雜誌》攜手 5 位在 AI 領域的專業講師,打造上述 6 堂實用課程,教你學會時下最好用的 AI 工具,導入生成式 AI 來產製工作內容,改造並升級你的工作流程。

輸入折扣碼 ZERO2024 還可以額外獲得 NT$400 優惠喔。

HTML與SEO實戰應用—並以ChatGPT助力提升網站品質與流量

HTML與SEO實戰應用—並以ChatGPT助力提升網站品質與流量

本課程專為希望深入了解 HTML 並有效結合 SEO 策略的學員設計。我們將重點放在 HTML 的深度學習與應用上,同時穿插介紹如何透過搜索引擎優化提升網站能見度。透過即時互動式的直播教學,加上 ChatGPT 的輔助,您將學習到如何建立一個結構優良、美觀且符合 SEO 標準的網站。這不僅會提升網站的用戶體驗,還會大幅提高網站的搜索引擎排名,進而增加訪客流量和潛在客戶。
用AI強化職場競爭力 ChatGPT、Midjourney從入門到精通

用AI強化職場競爭力 ChatGPT、Midjourney從入門到精通

在快速變遷的職場中,提升競爭力成為關鍵。透過引領潮流的AI技術,ChatGPT和Midjourney將助您勇攀高峰。無論您是AI新手還是專家,這個課程將引導您從入門到精通,解密AI的奧秘,並學習如何運用於職場。
GitHub Copilot AI 程式碼編輯工具應用實務班

GitHub Copilot AI 程式碼編輯工具應用實務班

讓學員瞭解有效地使用該工具來加速開發流程、提高程式碼品質和生產力。課程重點放在以 JavaScript 程式語言為例,介紹 Copilot 的基本原理、使用方法和最佳實踐。

輸入折扣碼 TC1456JA 還可以額外獲得 NT$500 優惠喔。

ChatGPT X Clipchamp AI 生成影片、配音與字幕應用實戰班

ChatGPT X Clipchamp AI 生成影片、配音與字幕應用實戰班

掌握Clipchamp AI的操作技巧,靈活運用Clipchamp AI進行影片編輯和創作,實現創意表達和傳播目的。

輸入折扣碼 TC1451JAN 還可以額外獲得 NT$500 優惠喔。

如何串接多種數位工具資訊?Looker Studio 資料視覺化實戰班|GoogleAds x FB廣告 x GA流量數據

如何串接多種數位工具資訊?Looker Studio 資料視覺化實戰班|GoogleAds x FB廣告 x GA流量數據

Looker Studio除了可協助使用者監控網站流量、廣告成效、選擇匯入資源的管道之外,還可以將數據資料多平台整合、數據報表即時更新、數據範本可重複套用的效益,透過自動化系統,將數據全部匯入同一個報表平台,是企業不可或缺的重要工具。

輸入折扣碼 TC1270JIA 還可以額外獲得 NT$500 優惠喔。

和我們交流

加入我們的社群,裡面會有一些技術的內容、有趣的技術梗,以及職缺的分享,歡迎和我們一起討論。