Technology

How to display subcategories on the category page in WordPress

2025-03-24 02:41:04


Fortunately, WordPress has a category and subcategory structure that helps you organize content systematically. 

Displaying subcategories on the main category page can make it easier for users to find the topics they want.


In this article, we will introduce how to display subcategories on the category page in WordPress, along with sample code and step-by-step instructions to improve your website's navigation.




Why should subcategories be displayed on the category page?

Properly categorizing content not only makes your website look organized but also helps with SEO and enhances the user experience for visitors.


For example, if you have a main category called "Sports," you might have subcategories like "NFL," "NBA," and "MLB." When customers go to the "Sports" category page, they will see all posts related to sports, but they won't be able to conveniently browse only the NFL or NBA.


If you display a list of subcategories on the main category page, users can easily click on the topics they are interested in, making navigation on your website smoother.



How to display subcategories on the category page in WordPress

You can display subcategories on the category page by adding code to your theme file. If you are not familiar with adding code in WordPress, please refer to our guide on how to add code in WordPress.


Step 1: Edit the category.php file

  • Go to your theme folder and look for the category.php file.
  • If this file does not exist, copy archive.php and rename it to category.php.
  • Open the category.php file and add the code below before the theme loop starts:

<?php

if (is_category()) {

    $this_category = get_category($cat);

}

if ($this_category->category_parent) {

    $this_category = wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.'&echo=0');

} else {

    $this_category = wp_list_categories('orderby=id&depth=1&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.'&echo=0');

}

if ($this_category) { ?>

    <ul>

        <?php echo $this_category; ?>

    </ul>

<?php } ?>


Step 2: Record and review the results

After adding this code, save the file and go to the category page on your website. You will see the subcategory list displayed.



Example of usage

  • On our sample website, the "Sports" page will display the subcategories "NFL," "NBA," and "MLB," allowing users to conveniently select and view only the relevant content.
  • When clicking on "NFL," users will be taken to the NFL category page and will still see links to other subcategories, making it easy to switch to "NBA" or "MLB."




Displaying subcategories on the main category page in WordPress is a great way to make your website more user-friendly by using simple code.  You can improve navigation and help users find the content they need more quickly.

Leave a comment :

Other interesting articles

There are many other interesting articles, try selecting them from below.