How to Display Clickable List of Custom Taxonomy in WordPress
As a WordPress developer, you may often encounter the need to display a list of taxonomy terms associated with a custom post type. The default the_category()
function in WordPress only retrieves terms from the 'category' taxonomy, as you can see in the core file https://core.trac.wordpress.org/browser/tags/4.9.2/src/wp-includes/category-template.php on line 75.
This can be a problem if you have created a custom taxonomy for your custom post type and want to display a clickable list of those terms. In this article, we'll explore two effective solutions to overcome this limitation and learn how to display a clickable list of custom taxonomy terms in WordPress.
Solution 1: Use the get_the_terms() function
The first solution involves using the built-in get_the_terms()
function in WordPress. This function allows you to retrieve the terms associated with a specific taxonomy for a given post.
Here's an example of how you can use this function to display a clickable list of custom taxonomy terms:
$terms = get_the_terms( $post->ID, 'taxonomyname' );
if ( $terms && ! is_wp_error( $terms ) ) {
echo '<div class="taxonomy-terms">';
foreach ( $terms as $term ) {
echo '<a href="' . get_term_link( $term->term_id ) . '">' . $term->name . '</a> ';
}
echo '</div>';
}
Let's break down the code:
-
$terms = get_the_terms( $post->ID, 'taxonomyname' );
: This line retrieves the terms associated with the 'taxonomyname' taxonomy for the current post. Replace 'taxonomyname' with the actual name of your custom taxonomy.
-
if ( $terms && ! is_wp_error( $terms ) ) { ... }
: This checks if there are any terms associated with the post and if the function call was successful (not returning a WordPress error).
-
echo '<div class="taxonomy-terms">';
: This opens a container div for the taxonomy terms.
-
foreach ( $terms as $term ) { ... }
: This loop iterates through each term and displays a clickable link to the term's archive page.
-
echo '<a href="' . get_term_link( $term->term_id ) . '">' . $term->name . '</a> ';
: This line creates a clickable link to the term's archive page, using the term's name as the link text.
-
echo '</div>';
: This closes the container div for the taxonomy terms.
This solution is straightforward and easy to implement. However, it may not be the most efficient approach if you have a large number of custom taxonomies or if you need to display terms from multiple taxonomies.
Solution 2: Create a Custom Function
Another solution is to create a custom function that can handle the display of clickable custom taxonomy terms. This approach allows you to encapsulate the logic and make it reusable across your WordPress project.
Here's an example of a custom function that displays a clickable list of custom taxonomy terms:
function display_custom_taxonomy_terms( $post_id, $taxonomy_name ) {
$terms = get_the_terms( $post_id, $taxonomy_name );
if ( $terms && ! is_wp_error( $terms ) ) {
echo '<div class="taxonomy-terms">';
foreach ( $terms as $term ) {
echo '<a href="' . get_term_link( $term->term_id, $taxonomy_name ) . '">' . $term->name . '</a> ';
}
echo '</div>';
}
}
To use this function, you can call it in your WordPress template file, like this:
display_custom_taxonomy_terms( $post->ID, 'taxonomyname' );
Replace 'taxonomyname' with the name of your custom taxonomy.
This custom function provides a few advantages:
-
Reusability: You can call the display_custom_taxonomy_terms()
function anywhere in your WordPress project, making it easy to display custom taxonomy terms for different post types or taxonomies.
-
Flexibility: The function accepts two parameters: $post_id
and $taxonomy_name
. This allows you to use it with any post ID and any custom taxonomy name, making it more versatile.
-
Consistency: By encapsulating the logic in a custom function, you can ensure a consistent way of displaying custom taxonomy terms across your website.
Both solutions presented in this article provide effective ways to display a clickable list of custom taxonomy terms in WordPress. The choice between the two approaches will depend on your specific needs and the complexity of your WordPress project.
If you have a relatively simple use case and only need to display terms from a single custom taxonomy, the first solution using get_the_terms()
may be the more straightforward option. However, if you have multiple custom taxonomies or need more flexibility and reusability, the custom function approach may be the better choice.
Regardless of the solution you choose, the key is to understand the limitations of the built-in the_category()
function and find a way to overcome them to ensure your WordPress website displays custom taxonomy terms as desired.
For more information on working with custom taxonomies in WordPress, be sure to check out the official WordPress documentation. And if you're looking for a tool to help you analyze and optimize your website's performance, consider Flowpoint.ai, a powerful web analytics platform that can identify technical issues and provide AI-generated recommendations to improve your conversion rates.