How to Display Taxonomy Terms in an Unordered List in WordPress

Home » Snippets » How to Display Taxonomy Terms in an Unordered List 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 your taxonomy terms in an organized and visually appealing way on your WordPress website? By default, WordPress displays taxonomy terms in a plain list format. However, with a few simple steps, you can easily customize the display of your taxonomy terms by placing them in an unordered list. In this article, we will guide you through the process of creating an unordered list for your taxonomy terms, enhancing the overall user experience and aesthetic of your website.

					function wpturbo_display_taxonomy_terms() {
    $taxonomy = 'category'; // Replace 'category' with your desired taxonomy
    $terms = get_terms($taxonomy);

    if ($terms) {
        echo '<ul>';

        foreach ($terms as $term) {
            echo '<li>' . $term->name . '</li>';
        }

        echo '</ul>';
    }
}

add_action('wp_footer', 'wpturbo_display_taxonomy_terms');
				

The code snippet provided allows you to display the taxonomy terms of a specific taxonomy in an unordered list format.

To begin, we define the function wpturbo_display_taxonomy_terms(). Within this function, we set the desired taxonomy by assigning the taxonomy name to the $taxonomy variable. In this example, we are using the 'category' taxonomy, but you can replace it with the name of the taxonomy you want to display.

Next, we use the get_terms() function to retrieve all the terms of the specified taxonomy. The get_terms() function takes the taxonomy name as a parameter and returns an array of term objects.

We then check if there are any terms returned by the get_terms() function using an if statement. If there are terms, we proceed to display them in an unordered list.

To output the list, we begin by echoing an opening <ul> tag. This tag is used to define the start of an unordered list.

We then use a foreach loop to iterate through each term object in the $terms array. Within the loop, we echo an opening <li> tag followed by the term name ($term->name) and a closing </li> tag. This effectively displays each term as a list item.

Finally, we echo a closing </ul> tag to mark the end of the unordered list.

By using the add_action() function, we hook the wpturbo_display_taxonomy_terms() function to the wp_footer action. This ensures that the taxonomy terms will be displayed at the bottom of the website’s footer on every page.

You can customize this code snippet by replacing 'category' with the name of any other taxonomy on your WordPress website. Additionally, you can modify the HTML markup inside the foreach loop to suit your specific design requirements.

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