get_the_terms 是什么?
get_the_terms()
函數(shù)用于WordPress 中檢索與特定帖子或自定義帖子類型相關(guān)的術(shù)語(如類別、標(biāo)簽或任何其他分類法)。該函數(shù)返回一個術(shù)語對象數(shù)組,開發(fā)者可以精確地操作和顯示這些分類術(shù)語,以便更好地控制它們在網(wǎng)站上的呈現(xiàn)方式。
![圖片[1]-如何使用 get_the_terms() 在 WordPress 中管理和顯示分類術(shù)語](http://gqxi.cn/wp-content/uploads/2025/02/20250211144856673-image.png)
通過 get_the_terms()
,可以輕松地獲取與帖子相關(guān)的分類術(shù)語,進(jìn)而根據(jù)需要顯示、修改或管理這些術(shù)語。它是 WordPress 中非常有用的一個功能,可以讓你以編程的方式操作分類數(shù)據(jù),從而使網(wǎng)站內(nèi)容更加豐富和結(jié)構(gòu)化。
get_the_terms 的常見用例
get_the_terms()
在實(shí)際應(yīng)用中非常靈活,下面是一些常見的使用場景:

顯示帖子頁面上的類別或標(biāo)簽
在 WordPress 中,最常見的用法之一是檢索并顯示帖子頁面上的類別或標(biāo)簽。以下是一個例子,展示了如何使用 get_the_terms()
獲取并顯示與某個帖子相關(guān)的類別:
$categories = get_the_terms( $post->ID, 'category' );
if ( $categories && ! is_wp_error( $categories ) ) {
$category_list = array();
foreach ( $categories as $category ) {
$category_list[] = $category->name;
}
echo implode( ', ', $category_list );
}
此代碼段會列出與指定帖子相關(guān)的所有類別,并以逗號分隔。
獲取多個分類法
如果你需要檢索多個分類法(例如同時檢索“類別”和“標(biāo)簽”),可以將分類法名稱作為數(shù)組傳遞給 get_the_terms()
:
$terms = get_the_terms( $post->ID, array( 'category', 'post_tag' ) );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
echo '<p>' . $term->name . '</p>';
}
}
這段代碼會顯示指定帖子關(guān)聯(lián)的所有分類法術(shù)語,不論它們是類別還是標(biāo)簽。
使用自定義分類法
如果你使用的是自定義分類法,get_the_terms()
也能方便地幫助你獲取相關(guān)的術(shù)語。例如,假設(shè)你有一個名為“類型”的自定義分類法,分配給一個名為“電影”的自定義帖子類型:
$genres = get_the_terms( $post->ID, 'genre' );
if ( $genres && ! is_wp_error( $genres ) ) {
foreach ( $genres as $genre ) {
echo '<p>' . $genre->name . '</p>';
}
}
為了更好地組織這些電影內(nèi)容,你創(chuàng)建了一個名為“劇情”的自定義分類法,來對電影進(jìn)行分類(如“動作”,“喜劇”,“科幻”等)。

處理沒有術(shù)語的情況
如果某個帖子沒有與之關(guān)聯(lián)的術(shù)語,或者出現(xiàn)錯誤,get_the_terms()
會返回 false
。為了避免潛在問題,檢查 false
是一個好的實(shí)踐:
$terms = get_the_terms( $post->ID, 'category' );
if ( $terms && ! is_wp_error( $terms ) ) {
// 顯示術(shù)語
} else {
echo '未找到任何術(shù)語。';
}
自定義術(shù)語輸出
當(dāng)使用 get_the_terms()
獲取術(shù)語后,你可以根據(jù)需要定制其顯示方式。例如,可以為術(shù)語添加鏈接、應(yīng)用樣式或顯示與術(shù)語相關(guān)的其他信息。以下是一個將術(shù)語鏈接到其分類存檔頁面的例子:
$terms = get_the_terms( $post->ID, 'category' );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
echo '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a>';
}
}
通過簡單的自定義,可以為訪問者提供更加友好的術(shù)語呈現(xiàn)。
處理術(shù)語計數(shù)和層次分類法
在使用分類法時,展示更多關(guān)于術(shù)語的詳細(xì)信息(例如術(shù)語的層次結(jié)構(gòu)或每個術(shù)語下的帖子數(shù)量)非常有幫助。以下是一些處理術(shù)語計數(shù)和層次關(guān)系的方法:
- 層次分類法
例如,在類別這種分層分類法中,術(shù)語可能有父子關(guān)系。你可以檢查術(shù)語的父術(shù)語 ID,并據(jù)此顯示其層級關(guān)系:
$categories = get_the_terms( $post->ID, 'category' );
if ( $categories && ! is_wp_error( $categories ) ) {
foreach ( $categories as $category ) {
if ( $category->parent != 0 ) {
echo '<strong>' . $category->name . '</strong> (Parent)';
} else {
echo $category->name;
}
}
}
- 術(shù)語計數(shù)
另一個常見的需求是顯示與每個術(shù)語相關(guān)聯(lián)的帖子數(shù)量,這對于展示某些術(shù)語的流行度非常有用:
$tags = get_the_terms( $post->ID, 'post_tag' );
if ( $tags && ! is_wp_error( $tags ) ) {
foreach ( $tags as $tag ) {
echo $tag->name . ' (' . $tag->count . ') ';
}
}
這段代碼會顯示標(biāo)簽名稱,并附上該標(biāo)簽下的帖子數(shù)量。
![圖片[4]-如何使用 get_the_terms() 在 WordPress 中管理和顯示分類術(shù)語](http://gqxi.cn/wp-content/uploads/2025/02/20250211150749534-image.png)
性能注意事項
盡管 get_the_terms()
是一個非常強(qiáng)大的工具,但在處理大型網(wǎng)站或包含大量術(shù)語的帖子時,性能仍然需要考慮。以下是一些性能優(yōu)化建議:
- 緩存術(shù)語結(jié)果:特別是在高流量頁面或循環(huán)中,緩存術(shù)語檢索結(jié)果可以顯著提升性能。
- 避免不必要的查詢:如果術(shù)語數(shù)據(jù)不會頻繁更改,避免頻繁調(diào)用
get_the_terms()
,以減少對數(shù)據(jù)庫的壓力。
常見問題解答
- 如何使用
get_the_terms()
檢索類別或標(biāo)簽?
只需傳遞帖子 ID 和分類法(如“類別”或“帖子標(biāo)簽”)即可檢索相關(guān)術(shù)語。 - 如何顯示標(biāo)簽或其他分類法的術(shù)語計數(shù)?
通過get_the_terms()
返回的術(shù)語對象,可以輕松顯示與每個術(shù)語關(guān)聯(lián)的帖子數(shù)量。 get_the_terms()
可以與自定義分類法一起使用嗎?
是的,get_the_terms()
完全支持自定義分類法,你可以像使用內(nèi)置分類法一樣使用它來檢索任何自定義分類法的術(shù)語。- 如何定制
get_the_terms()
檢索到的術(shù)語輸出?
你可以添加鏈接、樣式或其他自定義元素,使術(shù)語顯示更加符合你的網(wǎng)站需求。
聯(lián)系我們 | |
---|---|
教程看不懂?聯(lián)系我們?yōu)槟赓M(fèi)解答!免費(fèi)助力個人,小企站點(diǎn)! |
![]() 客服微信
|
① 電話:020-2206-9892 | |
② QQ咨詢:1025174874 | |
③ 郵件:info@361sale.com | |
④ 工作時間:周一至周五,9:30-18:30,節(jié)假日休息 |
暫無評論內(nèi)容