Output WordPress categories in the theme

Each WordPress post is assigned at least one, often several categories. This short tutorial explains how you can display WordPress categories in the theme, both for the respective post and outside the WordPress loop.

Show WordPress categories of the post

We get the categories for a certain post with get_the_category_list () .

 $categories = get_the_category_list();

If we call the function without parameters, we get an unordered HTML list of the categories. Not all but only the categories of the respective contribution are output. The function should therefore be called within the WordPress loop.

As a theme developer, it's best to create your own template function to output the categories of a post. This could look something like this:

 if ( ! function_exists( 'theme_slug_entry_category' ) ) : /** * Displays categories. */ function theme_slug_entry_category() { // Get categories. $categories_list = get_the_category_list(); // Display categories. if ( $categories_list ) { echo $categories_list; } } endif;

By using your own theme function, the output of the categories can be designed at a central point and used in several places in the theme (blog, archive, single view). The function_exists wrapper ensures that child themes can easily overwrite the function.

Since we are not get_the_category_list() any arguments to get_the_category_list() , we get an unordered list as mentioned earlier.

 <ul class="post-categories"> <li><a href=".../category/allgemein/" rel="category tag">Allgemein</a></li> <li><a href=".../category/news/" rel="category tag">News</a></li> </ul>

We can now design this as we like with a little CSS code in our style.css.

Output WordPress categories as simple links

By specifying a space as the first parameter, we can also output the categories as individual links instead of a list. In addition to a space, the categories can also be separated with a comma as a separator.

 $categories = get_the_category_list( ', ' );

For example, a line of text “Posted in Category A , Category B ” can be displayed. You can adjust the template function in your theme as follows:

 function theme_slug_entry_category() { // Get categories. $categories_list = get_the_category_list( ', ' ); // Check if there are categories. if ( $categories_list ) { // Display categories. printf( '<span class="entry-categories">' . esc_html__( 'Posted in %1$s', 'theme-slug' ) . '</span>', $categories_list ); } }

The following HTML code is output:

 <span class="entry-categories"> Posted in <a href=".../category/allgemein/" rel="category tag">Allgemein</a>, <a href=".../category/news/" rel="category tag">News</a> </span>

Normally you will only need the first parameter of get_the_category_list () . However, if you ever want to list the categories of a very specific post outside of the WordPress loop, you can use the third parameter to specifically transfer a Post ID.

List all WordPress categories in the theme

So far, we've learned how to view the assigned categories of a single post.

You may also want to list all categories of the complete WordPress installation in the theme - for example as static navigation in the footer, in a sitemap page template or as suggestions within the 404 template.

The wp_list_categories () function is available for this application.

 wp_list_categories();

The function takes an array with arguments. A full list of options can be found in the WordPress Developers Handbook linked above. For example, the number of posts per category can be excluded with show_count or categories with exclude :

 $category_args = array( 'orderby' => 'name', 'show_count' => true, 'exclude' => array( 10, 42 ), ); wp_list_categories( $category_args );

By default, the categories are output directly, but the parameter echo can also be set to false , with which the categories can be returned as an HTML string and processed further.

Conclusion

With these two functions, as a theme developer, most use cases for listing all WordPress categories can be mapped as navigation, list or simple links.