How to Display Subcategories on Category Pages in WordPress

WordPress is a powerful content management system that is used to create dynamic websites and blogs. One of its best features is the ability to display sub-categories in the category pages. This allows for organized navigation for your visitors, providing them with a better understanding of your website’s content. In this guide, we will explain how you can easily display sub-categories on category pages in WordPress.

Step 1: Understanding your Categories

The first step to displaying sub-categories in WordPress is understanding your categories. Categories help to organize your content on WordPress. Before you can display the sub-categories in a category page, you have to have at least one category and some sub-categories. A good rule of thumb is to create categories that are relevant to your content.

Step 2: Adding Sub-Categories

The next step is to add sub-categories to the category page. To do this, you can use the Category tab in the dashboard. From there, you can make a sub-category for the existing category.

Step 3: Editing the Template

After adding the sub-categories, you will need to edit the template. If you are using a WordPress theme, you should be able to find a category.php file. This file contains the code used to display your category page. To add the sub-categories to the page, you simply need to add the following code:

<?php
if ($category->category_parent > 0){
$subcategories = get_categories('child_of='.$category->cat_ID);
if (count($subcategories) > 0){
foreach ($subcategories as $subcategory){
echo '<li><a href="'.get_category_link($subcategory->cat_ID).'">'.$subcategory->name.'</a></li>';
}
}
}
?>

Step 4: Previewing the Result

Once you have added the code and saved the template, go ahead and preview the page. You should now see the sub-categories displayed on the page.

FAQ

How do I add sub-categories in WordPress?

To add sub-categories, go to the Categories section of your dashboard and select the category that you want to add a sub-category to. Then click 'Add New Sub-Category' and add a title.

How do I display the sub-categories?

To display the sub-categories, you will need to edit the template file and add a few lines of code. Once you have added the code and saved the template, preview the page to see the sub-categories displayed on the page.

Can I add sub-categories to sub-categories?

Yes, you can add sub-categories to sub-categories. To do this, go to the Categories section of the dashboard, select the sub-category that you want to add a sub-category to, and click ‘Add New Sub-Category’.

Can I add a different template for each category page?

Yes, you can use different templates for each category page. To do this, you will need to create a separate file for each category page, using the slug of the category as the file name. Then, you will need to add the code for the template in the file.

What if I want to add a custom template for a specific category page?

You can easily create a custom template for specific category page. First, you will need to create a file with the slug of the category as the file name. Then, you will need to add the code for the custom template in the file. Finally, you will need to include the custom template in the Theme Functions file.

Additional Resources

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.