If you have a WordPress website and you want to display the grandchildren categories of a particular parent category, you're in the right place. In this article, we'll walk you through the process of displaying grandchildren categories in WordPress, step by step. We'll use a combination of WordPress's built-in features and a little bit of custom code to get the job done.
Understanding the Category Hierarchy in WordPress
Before we dive into the specifics of displaying grandchildren categories, it's important to understand the category hierarchy in WordPress. Here's a quick breakdown:
- Parent categories: These are the top-level categories in your WordPress site. They can contain child categories, but they don't have to.
- Child categories: These are categories that are nested within a parent category. They can contain grandchild categories, but they don't have to.
- Grandchild categories: These are categories that are nested within a child category. They are the third level of the category hierarchy.
In this article, we'll be focusing on displaying grandchild categories. But before we do that, let's take a look at how to create parent, child, and grandchild categories in WordPress.
Creating Parent, Child, and Grandchild Categories in WordPress
To create a parent category, follow these steps:
- Log in to your WordPress dashboard.
- Navigate to "Posts" > "Categories".
- Enter a name for your parent category in the "Name" field.
- Click "Add New Category" to create the category.
To create a child category, follow these steps:
- Navigate to "Posts" > "Categories".
- Enter a name for your child category in the "Name" field.
- Select the parent category from the "Parent Category" dropdown.
- Click "Add New Category" to create the category.
To create a grandchild category, follow these steps:
- Navigate to "Posts" > "Categories".
- Enter a name for your grandchild category in the "Name" field.
- Select the child category from the "Parent Category" dropdown.
- Click "Add New Category" to create the category.
Displaying Grandchildren Categories in WordPress
Now that we've created our parent, child, and grandchild categories, let's take a look at how to display the grandchildren categories on a page or post in WordPress. We'll use a combination of WordPress's built-in functions and a little bit of custom code to get the job done.
First, let's take a look at the code we'll be using to display the grandchildren categories. Add the following code to the page or post where you want to display the grandchildren categories:
<?php
$grandchild_categories = get_categories( array(
'parent' => ,
'hide_empty' => 0,
) );
if ( $grandchild_categories ) {
echo '
- ### Grandchild Categories
- ';
foreach ( $grandchild_categories as $grandchild_category ) {
echo '
' . $grandchild_category->name . '
';
}
echo '
';
}
?>
Let's break down what this code is doing:
-
$grandchild_categories = get_categories( array('parent' => $child_category_id,'hide_empty' => 0,) ); -
The
get_categories()function is a built-in WordPress function that retrieves a list of categories based on the parameters you pass to it. In this case, we're passing an array with two parameters:'parent' => $child_category_id: This parameter tells WordPress to only retrieve categories that are children of the category with the ID stored in the$child_category_idvariable. We'll talk more about how to set this variable in the next step.'hide_empty' => 0: This parameter tells WordPress to include categories that don't have any posts associated with them. If you want to exclude empty categories, you can set this parameter to1instead.
-
if ( $grandchild_categories ) {This line of code checks if the
$grandchild_categoriesvariable contains any categories. If it does, the code inside theifstatement will be executed. -
echo 'Grandchild Categories
';This line of code outputs the heading "Grandchild Categories" on the page or post.
-
foreach ( $grandchild_categories as $grandchild_category ) {This line of code starts a loop that iterates through each of the grandchild categories in the
$grandchild_categoriesarray. For each iteration of the loop, the code inside theforeachstatement will be executed. -
echo '- ' . $grandchild_category->name . '
';This line of code outputs the name of the grandchild category as a list item. The name of the category is stored in the
$grandchild_category->nameproperty. -
echo '
This line of code outputs the closing tag for the unordered list.
Now that we've looked at the code, let's take a look at how to set the $child_category_id variable. This variable is used to tell WordPress which child category to retrieve the grandchildren categories for. Here's how to set this variable:
-
If you want to display the grandchildren categories for a specific child category, you can set the
$child_category_idvariable to the ID of that child category. For example, if the ID of the child category is123, you would set the$child_category_idvariable like this:<?php $child_category_id = 123; ?> -
If you want to display the grandchildren categories for the current child category (i.e., the child category that's being viewed), you can set the
$child_category_idvariable like this:<?php $child_category = get_category( get_query_var( 'cat' ) ); $child_category_id = $child_category->category_parent; ?>This code uses the
get_category()andget_query_var()functions to retrieve the current child category and its ID. Theget_query_var()function retrieves the value of thecatquery variable, which is set to the ID of the current category. Theget_category()function then uses this ID to retrieve the category object.
In this article, we've shown you how to display grandchildren categories in WordPress using a combination of WordPress's built-in functions and a little bit of custom code. We hope this article has been helpful and that you're now able to display the grandchildren categories on your WordPress site. If you have any questions or comments, please leave them in the comments section below.
References
| Reference | Description |
|---|---|
| get_categories() | A built-in WordPress function that retrieves a list of categories based on the parameters you pass to it. |
| get_category() | A built-in WordPress function that retrieves a category object based on the ID you pass to it. |
| get_query_var() | A built-in WordPress function that retrieves the value of a query variable. |