Edit Content
1. What Are Taxonomies in WordPress?
3. Basic Syntax of get_the_terms()
4. Step-by-Step Guide to Displaying Taxonomies
4.1 Step 1: Retrieve Terms Within the Loop
4.2 Step 2: Display Tags Outside the Loop
4.3 Step 3: Display Custom Taxonomy Terms
5. Advanced Techniques for Enhanced Displays
5.1 Add Icons or Images to Terms
5.2 Filter and Sort Terms Dynamically
5.3 Optimize Performance with Caching
Taxonomies are one of the most powerful features in WordPress. They allow you to organize your content into categories, tags, or custom classifications, making it easier for users to navigate your site. Whether you’re running a blog, an e-commerce store, or a portfolio website, taxonomies play a critical role in enhancing user experience and improving SEO.
In this guide, we’ll explore how to use the get_the_terms() function in WordPress to display taxonomies effectively. By the end of this article, you’ll have a clear understanding of how to implement this function in your themes or plugins, along with practical examples and best practices.
Before diving into get_the_terms(), let’s take a moment to understand what taxonomies are and why they’re so important in WordPress.
In simple terms, taxonomies are a way to group and organize your content. Think of them as labels or categories that help you classify posts, pages, or custom post types based on shared characteristics. They play a crucial role in improving both user experience and SEO by making your site easier to navigate and understand.
Categories are hierarchical taxonomies , meaning they can have parent-child relationships. For example, you might have a “Recipes” category with subcategories like “Breakfast,” “Lunch,” and “Dinner.” Categories are ideal for grouping content into broader sections, providing a clear structure to your site.
Tags are non-hierarchical taxonomies that allow for more granular organization. Unlike categories, tags don’t have a structured hierarchy. For instance, a blog post about “WordPress SEO Tips” might include tags like “SEO,” “Plugins,” and “Performance.” Tags are great for highlighting specific topics or keywords within your content.
Custom taxonomies are user-defined classifications that you can create to suit your site’s unique needs. For example, if you run a movie review site, you might create a custom taxonomy called “Genres” with terms like “Action,” “Comedy,” and “Drama.” Custom taxonomies give you the flexibility to organize content in ways that make sense for your niche.
Taxonomies are more than just organizational tools—they’re essential for creating a seamless user experience and boosting your site’s SEO. By categorizing your content effectively:
In short, taxonomies are the backbone of content organization in WordPress. Mastering how to display and utilize them—using functions like get_the_terms()—can take your site to the next level.
The get_the_terms() function is a built-in WordPress function that retrieves the terms associated with a specific post or custom post type. It’s particularly useful because:
Compared to similar functions like wp_get_post_terms() or get_the_term_list(), get_the_terms() offers a balance of simplicity and power, making it ideal for most use cases.
Here’s the basic syntax of the get_the_terms() function:
<?php
$terms = get_the_terms( $post_id, $taxonomy );
?>
Now, let’s walk through how to use get_the_terms() to display taxonomies on your WordPress site.
If you’re working inside the WordPress loop, you can use get_the_ID() to automatically fetch the current post’s ID. Here’s an example:
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$terms = get_the_terms( get_the_ID(), ‘category’ );
if ( $terms && ! is_wp_error( $terms ) ) {
echo ‘<ul>’;
foreach ( $terms as $term ) {
echo ‘<li><a href=”‘ . esc_url( get_term_link( $term ) ) . ‘”>’ . esc_html( $term->name ) . ‘</a></li>’;
}
echo ‘</ul>’;
} else {
echo ‘<p>No categories found.</p>’;
}
endwhile;
endif;
?>
You can also use get_the_terms() outside the loop by specifying a post ID manually. For example:
<?php
$post_id = 42; // Replace with your desired post ID
$tags = get_the_terms( $post_id, ‘post_tag’ );
if ( $tags && ! is_wp_error( $tags ) ) {
echo ‘<div class=”tags”>’;
foreach ( $tags as $tag ) {
echo ‘<span class=”tag”><a href=”‘ . esc_url( get_term_link( $tag ) ) . ‘”>’ . esc_html( $tag->name ) . ‘</a></span>’;
}
echo ‘</div>’;
} else {
echo ‘<p>No tags found.</p>’;
}
?>
This approach is useful for displaying tags in sidebars, footers, or other non-loop areas.
For custom taxonomies, simply replace the taxonomy name. For instance, if you have a custom taxonomy called ‘genre’:
<?php
$genres = get_the_terms( get_the_ID(), ‘genre’ );
if ( $genres && ! is_wp_error( $genres ) ) {
echo ‘<ul class=”genres”>’;
foreach ( $genres as $genre ) {
echo ‘<li><a href=”‘ . esc_url( get_term_link( $genre ) ) . ‘”>’ . esc_html( $genre->name ) . ‘</a></li>’;
}
echo ‘</ul>’;
} else {
echo ‘<p>No genres found.</p>’;
}
?>
Unlock the full potential of your WordPress site with advanced taxonomy displays. By mastering techniques like adding icons or images to terms, dynamically filtering and sorting terms, and optimizing performance with caching, you can create visually stunning and highly functional designs. These methods not only enhance user experience but also improve site efficiency.
You can associate icons or images with terms using Advanced Custom Fields (ACF) or custom fields. For example:
<?php
$terms = get_the_terms( get_the_ID(), ‘category’ );
if ( $terms && ! is_wp_error( $terms ) ) {
echo ‘<div class=”categories-with-icons”>’;
foreach ( $terms as $term ) {
$icon_url = get_field( ‘category_icon’, $term ); // Assuming ACF field exists
echo ‘<div class=”category-item”>’;
if ( $icon_url ) {
echo ‘<img src=”‘ . esc_url( $icon_url ) . ‘” alt=”‘ . esc_attr( $term->name ) . ‘”>’;
}
echo ‘<a href=”‘ . esc_url( get_term_link( $term ) ) . ‘”>’ . esc_html( $term->name ) . ‘</a>’;
echo ‘</div>’;
}
echo ‘</div>’;
}
?>
You can filter terms based on criteria like count or name. For example, to display only the top 3 categories:
<?php
$terms = get_the_terms( get_the_ID(), ‘category’ );
if ( $terms && ! is_wp_error( $terms ) ) {
usort( $terms, function( $a, $b ) {
return $b->count – $a->count; // Sort by term count (descending)
});
$top_terms = array_slice( $terms, 0, 3 ); // Get top 3 terms
echo ‘<ul>’;
foreach ( $top_terms as $term ) {
echo ‘<li><a href=”‘ . esc_url( get_term_link( $term ) ) . ‘”>’ . esc_html( $term->name ) . ‘</a></li>’;
}
echo ‘</ul>’;
}
?>
To improve performance, especially on high-traffic sites, consider caching the results of get_the_terms() using WordPress transients:
<?php
$post_id = get_the_ID();
$cache_key = ‘post_terms_’ . $post_id;
$terms = get_transient( $cache_key );
if ( false === $terms ) {
$terms = get_the_terms( $post_id, ‘category’ );
set_transient( $cache_key, $terms, DAY_IN_SECONDS ); // Cache for 24 hours
}
if ( $terms && ! is_wp_error( $terms ) ) {
echo ‘<ul>’;
foreach ( $terms as $term ) {
echo ‘<li><a href=”‘ . esc_url( get_term_link( $term ) ) . ‘”>’ . esc_html( $term->name ) . ‘</a></li>’;
}
echo ‘</ul>’;
}
?>
To ensure your implementation is robust and maintainable, follow these best practices:
get_the_terms() retrieves an array of term objects, giving you full control over how to display or manipulate the data. In contrast, get_the_term_list() directly outputs a formatted HTML string of terms as links. Use get_the_terms() for customization and get_the_term_list() for quick, pre-styled displays.
Yes, absolutely! get_the_terms() works seamlessly with custom taxonomies. Simply specify the name of your custom taxonomy (e.g., ‘genre’ or ‘location’) as the second parameter. This flexibility makes it a powerful tool for organizing and displaying content tailored to your site’s unique structure.
Always check for errors by using is_wp_error() after calling get_the_terms(). If no terms are found or an issue occurs, the function may return false or a WP_Error object. Implementing proper error handling ensures your site remains functional and user-friendly, even when unexpected issues arise.
The get_the_terms() function is an indispensable tool for WordPress developers looking to display taxonomies effectively. By mastering its usage, you can create dynamic, user-friendly websites that are both visually appealing and SEO-optimized.
Whether you’re building a simple blog or a complex e-commerce platform, the techniques outlined in this guide will help you unlock the full potential of WordPress taxonomies. So go ahead—experiment with get_the_terms() and elevate your site’s content organization to the next level!
We encompass a wide range of solutions, including eCommerce development, WordPress development, mobile app development, and digital marketing.