How to List All Categories in WordPress: A Comprehensive Guide

Home » Snippets » How to List All Categories in WordPress: A Comprehensive Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Categories are one of the best ways to organize your content in WordPress. They provide an easy way for your visitors to navigate through and access the content they’re interested in. Being able to list all your categories in an organized way can help enhance the user experience on your website. Whether you want to create a categories page, add a categories list to your sidebar or simply have a quick reference for yourself, this article will walk you through the process of listing all categories on your WordPress site.

					function wpturbo_list_all_categories() {
    $categories = get_categories( array(
        'orderby' => 'name',
        'order'   => 'ASC'
    ) );

    foreach( $categories as $category ) {
        echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br />';
    } 
}
add_action('wp_footer', 'wpturbo_list_all_categories');
				

To understand this snippet, we must break it down into its separate components. We begin with the function wpturbo_list_all_categories(), which, when invoked, will retrieve and display all categories in your WordPress site.

Here, get_categories method is used, which returns a list of categories. This method accepts an associative array of arguments that define how to retrieve the categories. In this case, we are defining two arguments.

The first argument 'orderby' => 'name', arranges the list of categories by their name. The second argument, 'order' => 'ASC' sorts these categories in Ascending order. An ascending sort behaves in reverse alphabetical order so ‘Z’ comes before ‘A’. If you want to sort it in descending order [Z-A], you would replace ‘ASC’ with ‘DESC’.

The returned categories are stored in the $categories variable.

After that, for each category stored in $categories, we print an HTML link by using the foreach loop:

foreach( $categories as $category ) {
    echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br />';
} 

In this loop, get_category_link( $category->term_id ) is used to create the URL for each category, using their term ID (a unique identifier). The category name is printed using $category->name. The combination of this will create a hyperlinked category name that points to the respective category’s page when clicked.

Finally, the function wpturbo_list_all_categories() is hooked into the ‘wp_footer’ action, which adds the list of categories to your WordPress site’s footer.

Now, whenever the footer of your WordPress site is generated, it will include a list of all your categories listed alphabetically, with each category name linking to the respective category page.

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