How to Display Custom Taxonomy Tags Outside the Loop in WordPress

Home » Snippets » How to Display Custom Taxonomy Tags Outside the Loop in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to display custom taxonomy tags outside the loop in WordPress? By default, WordPress only provides functions to display taxonomy tags within the loop, making it challenging to showcase them in other areas of your website. However, with a few simple steps, you can easily display custom taxonomy tags outside the loop and customize their appearance to fit your website’s design. In this article, we will guide you through the process of displaying custom taxonomy tags outside the loop in WordPress.

					function wpturbo_display_custom_taxonomy_tags() {
    $terms = get_terms( 'your_taxonomy_name', array( 'hide_empty' => false ) );

    if ( ! empty( $terms ) && is_array( $terms ) ) {
        echo '<ul class="wpturbo-custom-taxonomy-tags">';
        foreach ( $terms as $term ) {
            echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
        }
        echo '</ul>';
    }
}
add_action( 'wp_footer', 'wpturbo_display_custom_taxonomy_tags' );
				

The code snippet provided allows you to display custom taxonomy tags outside the loop in WordPress. This means that you will be able to display these tags on any page or post, regardless of where the loop is located.

To accomplish this, the first step is to create a new function called wpturbo_display_custom_taxonomy_tags(). Inside this function, we will perform the necessary operations to retrieve and display the custom taxonomy tags.

The get_terms() function is used to retrieve all the terms from the specified taxonomy. In the code snippet, 'your_taxonomy_name' should be replaced with the actual name of your custom taxonomy. By setting the hide_empty parameter to false, we ensure that even the terms without any associated posts are included in the results.

Next, we check if the $terms variable is not empty and is an array. This check is important because if there are no terms found for the specified taxonomy, we don’t want to display an empty list.

If there are terms to display, we start by outputting the opening <ul> tag with a class of wpturbo-custom-taxonomy-tags. This class can be customized to match your theme’s styling.

We then loop through each term using a foreach loop. Inside the loop, we output each term as an <li> item with an <a> tag. The href attribute of the <a> tag is set using the get_term_link() function, which generates the URL for the term’s archive page. The name of the term is retrieved using the $term->name property.

After the loop is completed, we output the closing </ul> tag to properly close the unordered list.

Finally, we hook the wpturbo_display_custom_taxonomy_tags() function to the wp_footer action. This ensures that the function will be executed when rendering the footer of the page, allowing the custom taxonomy tags to be displayed outside the loop.

By adding this code snippet to your theme’s functions.php file or a custom plugin, you will be able to display your custom taxonomy tags anywhere on your WordPress website, providing more flexibility in showcasing your content.

Register an account to save your snippets or go Pro to get more features.