To display a list of categories with nested subcategories in WordPress, you can use the wp_list_categories()
function. This function generates an unordered list (<ul>
) with nested subcategories.
Here’s an example of how to use the function:
'name', 'order' => 'ASC', 'hide_empty' => true, // Set to false if you want to show empty categories 'hierarchical' => true, 'title_li' => '', // Set an empty title_li to remove the default 'Categories' title ); echo '<ul>'; wp_list_categories($args); echo '</ul>';
Place this code snippet in the appropriate theme template file where you want to display the list of categories with nested subcategories, such as sidebar.php
or index.php
.
By default, the wp_list_categories()
function generates a nested <ul>
structure for subcategories, and WordPress will apply the appropriate CSS classes for styling.
Remember to always backup your files before making any changes, and test your changes on a staging site or local development environment before applying them to your live site.