Home » Wordpress » WordPress 3 – parent/child category manipulation

WordPress 3 – parent/child category manipulation

Posted by: admin November 30, 2017 Leave a comment

Questions:

It is possible to show subcategories of parent categories in WordPress.

However, I have the need to show just parent categories, and then to display subcategories when the user clicks on the parent.

Let’s say I have category A and B. In A I have A1, A2 and A3. In B B1 and B2. Now I would like to show the categories on the frontend… something like:

Category A:
Category B:

When the user clicks on Category A, I want the following to occur:

Category A
- A1
- A2
- A3
Category B

When the user clicks Category B, I want the following to occur:

Category A
Category B
- B1
- B2

Does anyone know if this is possible please? If so, could you add some pseudo code that may help me understand please?

Answers:

I guess you are not asking how to get categories & sub-categories, but you want to know how to render html such that only parent categories are show and on click of parent sub categories should be displayed. For this you need to use accordion menus. You can style them as per your needs. So all the categories (parent & sub) will be render but will be displayed as you want.

Some examples of accordion menus, look at the html and you will get the idea how to render html in your case.

example 1
example 2