Introduction
This guide provides a detailed walkthrough on how to use the category_list shortcode in WordPress to dynamically display categories along with their subcategories. This shortcode is highly customizable and includes options to show or hide child categories, add toggles for expanding child categories, and control the visibility of the parent category.
Step 1: Implementing the PHP Code
First, you’ll need to add the PHP code that powers the shortcode. This code should be added using the Code Snippets plugin in WordPress, which allows you to insert PHP code directly through the WordPress admin panel without editing your theme files.
Instructions:
- Go to your WordPress dashboard.
- Navigate to Snippets > Add New.
- Title your snippet — for example, “Category List Shortcode”.
- Paste the PHP code for the shortcode into the provided box.
- Choose “Run snippet everywhere”.
- Save and activate the snippet.
PHP
add_shortcode('category_list', 'render_category_list');
function render_category_list($atts) {
$atts = shortcode_atts(array(
'taxonomy' => 'category',
'id' => '',
'parent' => 'yes',
'children' => 'yes',
'toggle' => 'yes'
), $atts, 'category_list');
$output = '';
$term = is_numeric($atts['id']) ? get_term_by('id', $atts['id'], $atts['taxonomy']) : get_term_by('slug', $atts['id'], $atts['taxonomy']);
if (!$term) {
return 'Category not found.';
}
if ($atts['parent'] === 'yes') {
$output .= '<h3 class="parent-category"><a href="' . get_term_link($term) . '">' . esc_html($term->name) . '</a></h3>';
}
$child_terms = get_terms(array(
'taxonomy' => $atts['taxonomy'],
'parent' => $term->term_id,
'hide_empty' => false
));
if (!empty($child_terms)) {
$output .= '<ul class="subcategory-list">';
foreach ($child_terms as $child_term) {
$output .= '<li><a href="' . get_term_link($child_term) . '">' . esc_html($child_term->name) . '</a>';
$grandchildren = get_terms(array(
'taxonomy' => $atts['taxonomy'],
'parent' => $child_term->term_id,
'hide_empty' => false
));
if (!empty($grandchildren) && $atts['children'] === 'yes' && $atts['toggle'] === 'yes') {
$output .= ' <span class="toggle-symbol">+</span>';
$output .= '<ul class="grandchild-list" style="display: none;">';
foreach ($grandchildren as $grandchild) {
$output .= '<li><a href="' . get_term_link($grandchild) . '">' . esc_html($grandchild->name) . '</a></li>';
}
$output .= '</ul>';
}
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
Adding JavaScript and CSS
For the shortcode to function properly with interactive elements like toggles, you’ll need to add some JavaScript and CSS. This can be done using the Divi Theme Builder.
Instructions:
- In your WordPress dashboard, go to Divi > Theme Builder.
- Open the Global Header or create one if it doesn’t already exist.
- Add a new Code module.
- Paste the following JavaScript and CSS into the module:
JavaScript
jQuery(document).ready(function() {
function toggleSubcategory(element) {
var grandchildList = jQuery(element).next('ul.grandchild-list');
if (grandchildList.is(':hidden')) {
grandchildList.slideDown();
jQuery(element).text('-');
} else {
grandchildList.slideUp();
jQuery(element).text('+');
}
}
jQuery('.toggle-symbol').on('click', function() {
toggleSubcategory(this);
});
jQuery('.toggle-symbol').hover(
function() { jQuery(this).css('opacity', '0.7'); },
function() { jQuery(this).css('opacity', '1'); }
);
});
* Place the previous code in between script tags:
<script>
THE CODE HERE
</script>
CSS
.toggle-symbol {
cursor: pointer;
transition: opacity 0.3s ease;
user-select: none;
margin-left: 10px;
}
.subcategory-list, .grandchild-list {
padding-left: 20px;
}
* Place the previous code in between style tags:
<style>
THE CODE HERE
</style>
Step 2: Understanding the Shortcode Attributes
The category_list shortcode is designed to be flexible and customizable, with several attributes that control its functionality. Here’s a breakdown of each attribute and how it affects the shortcode’s behavior:
SHORTCODE
[category_list taxonomy="category" id="value" parent="yes" children="yes" toggle="yes"]
taxonomy="category"
Specifies the taxonomy from which to fetch categories.
Examples:
- id=”category” (WordPress Post)
- id=”product_cat” (WooCommerce Product)
- id=”download_category” (Easy Digital Downloads Download)
- id=”project_category” (Divi Project)
…
id="value"
The unique identifier for the category you wish to display. This can be either the numeric ID or the slug of the category.
Examples:
- id=”123″
- id=”sunglasses”
parent="yes/no"
Determines whether to display the name of the parent category. Set to “yes” to show the parent category name or “no” to hide it.
Examples:
- parent=”yes”
- parent=”no”
children="yes/no"
Controls whether to display the subcategories directly under the parent category. Set to “yes” to show child categories or “no” to hide them.
Examples:
- children=”yes”
- children=”no”
toggle="yes/no"
Adds a “+” toggle next to each subcategory that has its own children, allowing users to expand or collapse the sub-subcategories.
Examples:
- toggle=”yes”
- toggle=”no”
Step 3: Using the Shortcode
Once you understand the attributes, you can use the category_list shortcode anywhere on your site. These attributes can be combined in any post, page, or widget to display categories and subcategories exactly as needed.
Example 1
[category_list taxonomy="category" id="sunglasses" parent="yes" children="yes" toggle="yes"]
Preview
Example 2
[category_list taxonomy="category" id="sunglasses" parent="yes" children="no" toggle="no"]
Preview
Example 3
[category_list taxonomy="category" id="sunglasses" parent="no" children="no" toggle="no"]
When you paste your shortcode into a Divi module, you can easily edit the styles of the resulting Category List, both for the parent category to which the `h3 title` styles are applied, and for the subcategories, which are formatted as bulleted lists.
🥳 That’s it!







