How to Add Subcategories in WordPress: A Step-by-Step Guide

Home » Snippets » How to Add Subcategories in WordPress: A Step-by-Step Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

WordPress categories and subcategories are a great way to organize your content and make it easy for your readers to navigate your site. However, if you’re new to WordPress, you may be wondering how to add subcategories to your site. In this article, we’ll show you a step-by-step guide on how to add subcategories to your WordPress site. We’ll also go over some best practices for organizing your categories and subcategories to make your site as user-friendly as possible.

					function wpturbo_add_subcategories() {
   $cat_id = get_cat_ID( 'Parent Category' );
   $args = array(
      'parent' => $cat_id,
      'hide_empty' => false,
   );
   $child_categories = get_categories( $args );
   ?>
   <ul class="wpturbo-subcategories">
      <?php foreach ( $child_categories as $category ) : ?>
         <li><a href="<?php echo get_category_link( $category->term_id ); ?>"><?php echo $category->name; ?></a></li>
      <?php endforeach; ?>
   </ul>
   <?php
}

add_shortcode( 'wpturbo_subcategories', 'wpturbo_add_subcategories' );
				

The wpturbo_add_subcategories() function is designed to display all subcategories for a given parent category in WordPress. This can be especially useful for larger websites that require a hierarchical structure of categories.

The function first calls the get_cat_ID() function and passes in the name of the desired parent category. This will retrieve the category ID for the parent category, which will be used in the next step of the function.

The next step is to define the parameters for the get_categories() function. This function takes an array of arguments that will be used to query the database for the desired categories. In this case, we set the parent parameter to the ID of the parent category we retrieved in the previous step, and we set hide_empty to false to ensure that all subcategories are included, regardless of whether they have posts assigned to them or not.

After we have retrieved the child categories, we begin constructing the HTML markup to display them. We use an unordered list (ul) with a class of wpturbo-subcategories to contain all of the subcategories. We then loop through each child category using a foreach loop.

For each child category, we create a list item (li) with a link to the category archive page. We start by calling the get_category_link() function with the category ID to retrieve the URL for the archive page. We then output the name of the category using $category->name.

Finally, we close the li and the foreach loop, and close the ul. At this point, the function is complete.

To make this function easy to use, we add a shortcode that calls the wpturbo_add_subcategories() function. This way, you can simply add the [wpturbo_subcategories] shortcode to any page or post to display the list of subcategories.

Register an account to save your snippets or go Pro to get more features.