How to Display Subcategories on Category Pages in WordPress

WordPress is a popular content management system that powers over 40% of websites on the internet. It is known for its flexibility, ease of use, and wide range of customization options. If you run an online store or blog, chances are you have a lot of content, and it’s important to organize it in a way that’s easy for your visitors to navigate. One way to do this is by displaying subcategories on category pages. In this blog post, we’ll go over how to display subcategories on category pages in WordPress.

Table of Contents:

Why Display Subcategories on Category Pages?

By default, WordPress displays all posts in a category on the category page. This can be overwhelming for visitors, especially if you have a lot of content in a category. Displaying subcategories on category pages allows visitors to quickly find what they’re looking for and navigate to the content they’re interested in.

For example, let’s say you run a clothing store and you have a category for “Women’s Clothing.” Under that category, you may have subcategories for “Dresses,” “Tops,” “Bottoms,” and “Accessories.” Displaying these subcategories on the “Women’s Clothing” category page makes it easy for visitors to quickly find what they’re looking for.

How to Display Subcategories on Category Pages?

There are a few different ways to display subcategories on category pages in WordPress. We’ll go over three methods: using a plugin, adding code to your theme, and using a page builder.

Method 1: Use a Plugin

Using a plugin is the easiest way to display subcategories on category pages. There are several plugins available that can do this, but we recommend using the Category Posts Widget plugin.

To get started, go to the Plugins page in your WordPress dashboard and search for “Category Posts Widget.” Install and activate the plugin.

Once the plugin is activated, go to Appearance > Widgets. Find the “Category Posts” widget and drag it to the “Category” widget area. You can customize the widget settings to display subcategories and posts from those subcategories.

Method 2: Add Code to Your Theme

If you’re comfortable with adding code to your theme, you can use the following code to display subcategories on category pages:

<?php
if (is_category()) {
    $current_category = get_query_var('cat');
    if (get_category_children($current_category) != "") {
        echo "<ul>";
        wp_list_categories('orderby=id&depth=1&show_count=0&title_li=&child_of='.$current_category);
        echo "</ul>";
    }
}
?>
Other Example:

Displaying Sub Category(Parent or Children Categories) in the Category page template by category slug with a shortcode function

<?php
// Displaying sub Category in Category page template
function sub_category_list_shortcode_fn() {
	
    $page_url = $_SERVER["REQUEST_URI"];
    $page_slug = basename($page_url);
	
	$theCatId = get_term_by( 'slug', $page_slug, 'tcproductcat' );
	$theCatId = $theCatId->term_id;
	$taxonomy_name = 'tcproductcat';
	$termchildren = get_term_children( $theCatId, $taxonomy_name );
	
	echo '<div class="tcproduct-tab">';
	if($termchildren){
		// displaying children terms
		foreach ( $termchildren as $child ) {
			$term = get_term_by( 'id', $child, $taxonomy_name );
			echo '<a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a>';
		}
	}else{
		// displaying parent terms
		echo get_term_parents_list($theCatId, $taxonomy_name, array( 'separator' => false ));
	}
	echo '</div>';
}
add_shortcode( 'subcategories', 'sub_category_list_shortcode_fn' );
?>

To add this code to your theme, go to Appearance > Theme Editor. Select your theme and find the “functions.php” file. Paste the code at the bottom of the file and save your changes.

This code checks if the current page is a category page and if it has child categories. If it does, it displays a list of those child categories.

Method 3: Use a Page Builder

If you’re using a page builder like Elementor, you can use the following steps to display subcategories on category pages:

  1. Create a new page and select the category page template.
  2. Add a new section to the page.
  3. Add the “Posts” widget to the section.
  4. In the widget settings, select the category you want to display and choose to display subcategories.
  5. Customize the widget settings as desired.

This method allows you to use the drag-and-drop functionality of the page builder to display subcategories on category pages.

Tips for Displaying Subcategories on Category Pages

Here are a few tips to keep in mind when displaying subcategories on category pages in WordPress:

  1. Choose a method that works best for your site and your
  2. Consider the design: When displaying subcategories on category pages, it’s important to consider the design of your site. Make sure that the subcategories are displayed in a way that’s easy to read and understand. Use clear and descriptive titles for your subcategories and use a font and color that’s easy to read.
  3. Use relevant images: Adding relevant images to your subcategories can help visitors quickly identify the content they’re interested in. For example, if you have a subcategory for “Summer Dresses,” you may want to add an image of a summer dress to help visitors quickly identify that category.
  4. Keep it organized: Make sure that your subcategories are organized in a logical and easy-to-understand way. Use clear and concise titles for your subcategories and consider using subcategories to further organize your content.
  5. Test and iterate: Once you’ve implemented subcategories on your category pages, it’s important to test and iterate. Pay attention to how visitors are using your site and make adjustments as needed. For example, you may find that visitors are clicking on certain subcategories more than others, which can help you prioritize and organize your content.

Conclusion

In conclusion, displaying subcategories on category pages can help improve the user experience on your site and make it easier for visitors to find the content they’re interested in. Whether you choose to use a plugin, add code to your theme, or use a page builder, make sure to consider the design, use relevant images, keep it organized, and test and iterate as needed. With these tips in mind, you can create a user-friendly and organized site that visitors will love.

Leave a Reply