How to List All Subcategories in WordPress: A Comprehensive Guide

Home » Snippets » How to List All Subcategories 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

Looking to expand the ways you navigate through your WordPress website? Displaying subcategories can be a fantastic way to give your readers a convenient path to discover related posts. Whether you’re running a blog or an online store, listing all subcategories can greatly improve your site’s navigation and user experience. This article will guide you through the process of how to list all subcategories on your WordPress site in a simple and efficient way.

					function wpturbo_list_all_subcategories() {
    $category_id = get_queried_object_id();
    $categories = get_categories( array( 
        'child_of' => $category_id,
        'hide_empty' => false,
    ));
 
    if($categories) {
        echo '<ul>';
        foreach($categories as $category) {
            echo '<li><a href="' . get_category_link($category->term_id) . '">' . $category->name .'</a></li>';
        }
        echo '</ul>';
    }
}
				

In the provided snippet, we’ve defined a function called wpturbo_list_all_subcategories(). This function will fetch and display all the subcategories of the current category when called.

Here’s the line by line breakdown of how the snippet works:

The function begins with this line of code:

$category_id = get_queried_object_id();

Here, get_queried_object_id() is a WordPress function that fetches the ID of the currently queried object. In the context of a category page, this function will return the ID of the current category being viewed.

Next, we use the get_categories() function, which retrieves a list of category objects. This function accepts an array of arguments which specifies the conditions to fetch the categories:

$categories = get_categories( array( 
    'child_of' => $category_id,
    'hide_empty' => false,
));

In this case, child_of is set to $category_id, meaning we only want categories that are children of the current category. Also, hide_empty is set to false, which means categories with no posts will still be included in the retrieved list.

The next part of the function checks whether any categories were returned by the get_categories() function:

if($categories) {

If at least one category object was returned, the function will output an unordered HTML list with each category as a list item.

The foreach loop through $categories array creates an list item with a link to the category page for the current category in the loop:

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

Here get_category_link($category->term_id) is used to generate the URL of each category’s page and $category->name fetches the name of the category.

And finally, the function closing tags:

    echo '</ul>';
}

If $categories array is not empty the output is a HTML unordered list of links to all the subcategories of the current category.

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