What is get_the_terms?
get_the_terms()
function is used in WordPress to retrieve terms (such as categories, tags, or any other taxonomy) associated with a specific post or custom post type. The function returns an array of term objects that developers can precisely manipulate and display theseClassification terminologyto better control how they are presented on the site.
![图片[1]-如何使用 get_the_terms() 在 WordPress 中管理和显示分类术语](https://www.361sale.com/wp-content/uploads/2025/02/20250211144856673-image.png)
pass (a bill or inspection etc) get_the_terms()
It is a feature in WordPress that allows you to easily fetch the taxonomy terms associated with a post and in turn display, modify or manage them as needed. It is a very useful feature in WordPress that allows you to programmatically manipulate the taxonomy data toMake the site richer and more structuredThe
Common use cases for get_the_terms
get_the_terms()
It is very flexible in practical applications, and the following are some common usage scenarios:

Showing categories or tags on the post page
One of the most common uses in WordPress is to retrieve and display the post page'sformmaybetab (of a window) (computing). Here is an example showing how to use the get_the_terms()
Gets and displays the categories associated with a particular post:
$categories = get_the_terms( $post->ID, 'categories' );
if ( $categories && ! is_wp_error( $categories ) ) {
$categories_list = array(); if ( $categories && !
foreach ( $categories as $category ) {
$category_list[] = $category->name;
}
echo implode( ', ', $category_list ); }
}
This snippet will list all the categories related to the specified post, separated by commas.
Get multiple taxonomies
If you need to search multiple taxonomies (e.g., search both "form"and"tab (of a window) (computing)"), you can pass the names of taxonomies as an array to the 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>';
}
}
This code displays all taxonomy terms associated with the specified post, whether they are categories or tags.
Use of customized taxonomies
If you are using a custom taxonomy.get_the_terms()
It can also come in handy to help you access related terms. For example, suppose you have a file named "typology" of a custom taxonomy assigned to a custom taxonomy named "cinematic" for custom post types:
$genres = get_the_terms( $post->ID, 'genre' );
if ( $genres && ! is_wp_error( $genres ) ) {
foreach ( $genres as $genre ) {
echo '<p>' . $genre->name . '</p>';
}
}
In order to better organize this film content, you have created a new website called "plots"Custom taxonomies to categorize movies (e.g. "action", "comedy", "sci-fi", etc.).

Dealing with the absence of terminology
If a post doesn't have a term associated with it, or if there's an error.get_the_terms()
will return false
. To avoid potential problems, check false
It's a good practice:
$terms = get_the_terms( $post->ID, 'category' );
if ( $terms && ! is_wp_error( $terms ) ) {
// Display the terms
} else {
echo 'No terms found.' ;
}
Customized terminology output
When using the get_the_terms()
After acquiring a term, you can customize its display as needed. For example, you can create a custom display for the termAdd link,Application StylesmaybeDisplay additional information related to the term. Here is an example of linking a term to its categorized archive page:
$terms = get_the_terms( $post->ID, 'category' );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
echo '<a href="/en/' . get_term_link( $term ) . '/">' . $term->name . '</a>';
}
}
With simple customization, you can provide visitors with a more friendly presentation of terms.
Dealing with term counts and hierarchical taxonomies
When using the taxonomy, show moreDetailed information on terminology(e.g. the hierarchy of terms or the number of posts under each term) is very helpful. Here are some ways to handle term counts and hierarchical relationships:
- hierarchical taxonomy
For example, in a hierarchical taxonomy like categories, terms may have parent-child relationships. You can check the parent term ID of a term and display its hierarchical relationship accordingly:
$categories = get_the_terms( $post->ID, 'categories' );
if ( $categories && ! is_wp_error( $categories ) ) {
foreach ( $categories as $category ) {
if ( $category->parent ! = 0 ) {
echo '<strong>' . $category->name . '</strong> (Parent)'; } else {
} else {
echo $category->name; } else { echo $category->name.
}
}
}
- Terminology count
Another common requirement is to show the number of posts associated with each term, which is useful for showing the popularity of certain terms:
$tags = get_the_terms( $post->ID, 'post_tag' );
if ( $tags && ! is_wp_error( $tags ) ) {
foreach ( $tags as $tag ) {
echo $tag->name . ' (' . $tag->count . ') ';
}
}
This code displays the tag name with the number of posts under that tag.
![图片[4]-如何使用 get_the_terms() 在 WordPress 中管理和显示分类术语](https://www.361sale.com/wp-content/uploads/2025/02/20250211150749534-image.png)
Performance Notes
(go ahead and do it) without hesitating get_the_terms()
is a very powerful tool, but performance still needs to be considered when working with large sites or posts that contain a lot of jargon. Here are some performance optimization suggestions:
- Caching terminology results: Especially on high-traffic pages or loops, caching term retrieval results can significantly improve performance.
- Avoiding unnecessary queries: If the terminology data will not be changed frequently.Avoid frequentinvocations
get_the_terms()
, to reduce the pressure on the database.
Frequently Asked Questions
- How to use
get_the_terms()
Retrieve categories or tags?
Simply pass in the post ID and taxonomy (e.g. "category" or "post tag") to retrieve the relevant term. - How do I display term counts for labels or other taxonomies?
pass (a bill or inspection etc)get_the_terms()
The returned term objects make it easy to display the number of posts associated with each term. get_the_terms()
Can it be used with custom taxonomies?
Yes.get_the_terms()
Full supportCustomized Taxonomy, you can use it as you would the built-in taxonomy to retrieve terms from any custom taxonomy.- How to customize
get_the_terms()
Retrieved term output?
You can add links, styles, or other custom elements to make the terminology display more in line with your site's needs.
Link to this article:https://www.361sale.com/en/33048The article is copyrighted and must be reproduced with attribution.
No comments