How to Display Subcategories on Category Pages in WordPress

Do you want to display subcategories on category pages in WordPress?

WordPress makes it easy to organize your posts using categories and subcategories. You can help your visitors navigate subcategories more easily by displaying them on category pages.

In this article, we will show you how to display subcategories on category pages in WordPress.

Display Subcategories on Category Pages in WordPress

Why Display Subcategories on Category Pages?

When you write posts for your WordPress website, you can optimize for search engines and improve site navigation by categorizing content properly. This will bring more traffic to your site and allow visitors to find the content they are looking for more easily.

On some websites, it makes sense to use subcategories to simplify the site’s structure.

For example, you might have a top-level (parent) category called Sports. You can then have subcategories under Sports called NFL, NBA, and MLB.

Example of Using Subcategories in WordPress

When your visitors click on the Sports category, they will see every post in the Sports category and its subcategories. However, they won’t see the list of subcategories. That makes it difficult to filter down to just the NFL or NBA posts.

Displaying a list of subcategories on your category pages can improve site navigation. That’s particularly true if you decided to hide subcategories in your website’s category widget.

Show Only Top Level Categories

Then, for example, visitors to your site can easily move from all Sports posts to just NFL. Better still, you can continue to display the same subcategories on the NFL page so they can easily switch from NFL to NBA.

That being said, let’s look at how to display subcategories on category pages in WordPress.

How to Display Subcategories on Category Pages in WordPress

To display subcategories on your category archive pages, you’ll need to add code to your theme files. If you need help adding code to your site, then refer to our beginner’s guide on how to paste snippets from the web into WordPress.

The first thing you need to do is go inside your theme’s folder and look for your category archive file called category.php.

If you don’t see that file, then you will have to create it. Simply duplicate archive.php and name it category.php.

Note: If your theme doesn’t include category.php or archive.php, then you are probably using a WordPress theme framework and will have to create category.php manually. A good starting point for learning how to create archive files is our guide on how to create custom archive pages in WordPress.

Now, simply add the following code to category.php just before the loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
if (is_category()) {
    $this_category = get_category($cat);
    }
    ?>
    <?php
    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 } ?>

Hosted with ❤️ by WPCode

1-click Use in WordPress

Add the Code Snippet to category.php Just Before the Loop

Now when you visit a category page, you will see a list of subcategories.

On our demo site, the Sports category page shows links to the NFL, NBA, and MLB subcategories.

Preview of Subcategories on Category Page

When you click on the NFL link, you are taken to the NFL subcategory page.

Here you can still see the links to the Sports subcategories, so you can easily switch between the different types of sports. This can really simplify navigation on your site.

Preview of Subcategories on Subcategory Page

We hope this tutorial helped you learn how to display subcategories on category pages in WordPress. You may also want to learn how to improve your website’s SEO or our expert pick of the best WordPress popup plugins.

Do you want to display subcategories on category pages in WordPress? WordPress makes it easy to organize your posts using categories and subcategories. You can help your visitors navigate subcategories more easily by displaying them on category pages. In this article, we will show you how to display subcategories on…

Do you want to display subcategories on category pages in WordPress? WordPress makes it easy to organize your posts using categories and subcategories. You can help your visitors navigate subcategories more easily by displaying them on category pages. In this article, we will show you how to display subcategories on…

Leave a Reply

Your email address will not be published. Required fields are marked *