How to Display Subcategories on Category Pages in WordPress

Introduction: Enhancing Category Pages with Subcategory Displays
WordPress category pages, by default, list posts assigned to that specific category. However, websites with a hierarchical category structure often benefit from displaying subcategories directly on the parent category page. This improves navigation, helps users find more specific content, and enhances the overall user experience. This article explores various methods for displaying subcategories on WordPress category pages, catering to different technical skill levels and website configurations.
Understanding the Benefits of Displaying Subcategories
Presenting subcategories on your main category pages offers several key advantages:
- Improved Navigation: Users can quickly navigate to more specific content within a category.
- Enhanced User Experience: A clear display of subcategories makes it easier for visitors to find what they’re looking for.
- Increased Engagement: By showcasing the breadth of content, you encourage users to explore more of your site.
- Better SEO: A well-structured category page with subcategories can improve your site’s search engine optimization.
Displaying subcategories allows users to drill down into the topics they are most interested in, preventing them from having to sift through a large list of unrelated posts. This is particularly useful for websites with extensive content libraries and well-defined category hierarchies.
Method 1: Utilizing the Category Widget
The simplest method involves leveraging the built-in “Categories” widget in WordPress. While this widget doesn’t directly display subcategories on a category page, it can be configured to show a hierarchical list of all categories and subcategories on your sidebar.
To implement this:
- Navigate to Appearance > Widgets in your WordPress dashboard.
- Find the “Categories” widget.
- Drag and drop the widget into your desired sidebar area.
- Configure the widget settings:
- Title: Give the widget a suitable title, such as “Categories” or “Browse Topics”.
- Display as dropdown: Uncheck this box to display a list of categories.
- Show post counts: Choose whether to display the number of posts in each category.
- Hierarchical: Ensure this box is checked to display subcategories under their parent categories.
- Show this category only: Leave this unchecked to display all categories.
- Save the widget settings.
While this method is easy, it displays all categories and subcategories on every page where the sidebar is displayed, not just on category pages. It is best suited for sites with a limited number of categories.
Method 2: Editing the Category Template File (Requires Coding Knowledge)
For more precise control over where and how subcategories are displayed, you can directly edit your theme’s category template file. This method requires some familiarity with PHP and WordPress theme structure.
- Identify your theme’s category template file: This file is typically named `category.php` or `archive.php`. If your theme doesn’t have a dedicated `category.php` file, it may use `archive.php` to handle category archives. You might need to create a `category.php` file if one doesn’t exist, by copying the content of `archive.php` and modifying it.
- Create a child theme: **Important:** Never directly edit your parent theme’s files. Always create a child theme to avoid losing your changes when the theme is updated.
- Copy the category template file to your child theme.
- Edit the category template file in your child theme.
- Add the following PHP code snippet within the loop where you want to display the subcategories:
“`php
term_id;
$args = array(
‘child_of’ => $category_id,
‘hide_empty’ => 0,
‘title_li’ => ”
);
$subcategories = get_categories( $args );
if ( $subcategories ) {
echo ‘
Subcategories
‘;
echo ‘
- ‘;
-
- This code retrieves the subcategories of the current category and displays them as a list of links.
- Customize the code: You can modify the HTML structure and styling to match your theme’s design.
- Save the changes to your child theme’s category template file.
This method offers the most flexibility but requires coding skills and carries the risk of breaking your site if the code is not implemented correctly. Always back up your files before making any changes.
Method 3: Using a Plugin
Several WordPress plugins simplify the process of displaying subcategories on category pages. These plugins often provide a user-friendly interface for customizing the appearance and behavior of the subcategory display.
Here are a few popular options:
- Category Posts & Pages: This plugin allows you to display subcategories and posts within a category in various layouts.
- Display Subcategories: A simple plugin specifically designed to display subcategories on category pages.
- WooCommerce Category Display: If you are using WooCommerce, this plugin offers specialized options for displaying subcategories within your product categories.
To use a plugin:
- Navigate to Plugins > Add New in your WordPress dashboard.
- Search for the plugin you want to use (e.g., “Category Posts & Pages”).
- Install and activate the plugin.
- Configure the plugin settings: Each plugin has its own settings page, where you can customize the appearance and behavior of the subcategory display. This often includes options for:
- Choosing the layout (e.g., list, grid).
- Displaying the subcategory description.
- Setting the number of subcategories to display.
- Customizing the styling.
- Insert the plugin’s shortcode or block into your category template or page. Many plugins provide a shortcode or Gutenberg block that you can use to insert the subcategory display into your category template file or directly onto a category page (if you are using a page builder).
Plugins offer a balance between ease of use and customization. They are a good option for users who want more control than the widget method but are not comfortable editing theme files directly.
Method 4: Utilizing a Page Builder (Elementor, Beaver Builder, Divi)
If you use a page builder like Elementor, Beaver Builder, or Divi, you can often create custom category page templates and display subcategories using the page builder’s modules.
The specific steps vary depending on the page builder you are using, but the general approach is as follows:
- Create a new template: In your page builder, create a new template that will be used for category pages.
- Design the template: Use the page builder’s drag-and-drop interface to design the layout of the category page.
- Add a “Categories” module: Look for a module that allows you to display categories. Many page builders have a built-in module for this purpose, or you can use a third-party addon.
- Configure the module settings: Configure the module to display subcategories of the current category. This may involve setting the “parent category” option to the current category ID.
- Assign the template to category pages: Most page builders allow you to assign templates to specific post types or categories. Assign the template you created to the “category” post type or to specific categories.
Page builders provide a visual way to design category pages and offer a high degree of customization. This method is suitable for users who are already familiar with using a page builder.
Customization Considerations
Regardless of the method you choose, consider these customization options to ensure the subcategory display aligns with your website’s design and user experience:
- Styling: Use CSS to style the subcategory list to match your theme’s fonts, colors, and spacing.
- Number of subcategories: Limit the number of subcategories displayed to prevent overwhelming the user.
- Description: Display the subcategory description to provide more context.
- Thumbnail images: Include thumbnail images for each subcategory to make the display more visually appealing.
- Order: Control the order in which subcategories are displayed (e.g., alphabetically, by post count).
Testing and Optimization
After implementing any of these methods, thoroughly test the category pages on different devices and browsers to ensure they display correctly. Monitor user behavior to see how visitors interact with the subcategory display and make adjustments as needed. Consider A/B testing different layouts and designs to optimize the user experience and improve engagement.
Conclusion: Choosing the Right Method
Displaying subcategories on category pages is a valuable way to improve navigation and user experience on your WordPress website. The best method depends on your technical skills, website configuration, and desired level of customization. Widgets are the easiest option, editing template files offers the most flexibility, plugins provide a balance between ease of use and customization, and page builders offer a visual design approach. By carefully considering your needs and implementing the appropriate method, you can create category pages that are both informative and user-friendly.
Related Topics by Tag- Beginners Guide on How to Add Twitter Cards in WordPress
- How to Check and Update to the Latest WordPress Version
- How to Replace ‘Add Title’ Placeholder Text in WordPress
- How to Display Random Posts in WordPress (Easy Tutorial)
- How to Add Caption to Images in WordPress (Beginner’s Tutorial)
- How to Edit a WordPress Homepage (Easily & Effectively)
- How to Show Different Menus to Logged in Users in WordPress
foreach( $subcategories as $subcategory ) {
echo ‘