Search code examples
wordpressparentchildren

WordPress - Display children category on page


I have a category list from A - Z. These categories are displayed on a page. When someone clicks on ex: "A": i want them to see all the children categories which are in "A". Is there a method of showing all these children categories?

Because at the moment it will show all the posts which are in the parent "A", instead of showing all the children categories.

Example:

A: -A1 -A2 -A3 B: -B1 -B2 -B3


Solution

  • Using the wp_list_categories() function will return a list of all categories.

    You can filter the child categories using the 'depth' parameter:

    • 0 - All Categories and child Categories (Default).
    • -1 - All Categories displayed in flat (no indent) form (overrides hierarchical).
    • 1 - Show only top level Categories
    • n - Value of n (some number) specifies the depth (or level) to descend in displaying Categories

    All the info is in the documentation: http://codex.wordpress.org/Template_Tags/wp_list_categories

    You can then use javascript to show/hide the child lists when a user clicks its parent list item.