0

Say a site has a category structure like:

  • Dogs
    • Boxer
      • Fawn
      • Brindle
    • Rottweiler
  • Cats
    • Calico
    • Siamese

Using it shows that and adds classes so that you can style it accordingly such as making sure sub levels are indented.

If you want to show just one hierarchy such as Dogs where the category Dogs is id 15 you could use:

                <?php wp_list_categories( array(
                    'title_li'           => '',
                    'child_of'         => 15,
                ) ); ?>

When doing that it does this however:

  • Boxer
  • Brindle
  • Fawn
  • Rottweiler

That is, it does show just the Dogs tree, but it doesn't add the children class to be able to indent sub categories accordingly and seems to order them all by name, rather than just top level name.

Should be:

  • Boxer
    • Fawn
    • Brindle
  • Rottweiler

Am I missing something? Also if there were a category under say Fawn, I don't want that level or any further depths to show. Tried using "depth => 2," and didn't work. Setting to 0 shows all, setting to any other number such as 1 or 2 such shows the lowest depth.

2 Answers 2

0

Add 'style' => 'list' to arguments:

wp_list_categories(array(
    'child_of' => 15,
    'title_li' => '',
    'hide_empty' => 0, //just in case if no posts in category
    'style' => 'list',
    ));

Your output should be:

Boxer

 ● Fawn
 ● Brindle

Rotweiler
2
  • That doesn't seem to make any change. I check the codex and the default for style is already "list". Still listing Boxer, Fawn, Brindle, Roteweiler as their own list items and organized alphabetical rather than sub categories under their parents. Commented Dec 3, 2018 at 1:57
  • True about 'style' argument. Still, I've tested it and got output stated in my answer. Maybe your theme, or some plugin messing up your <li> styles. Commented Dec 3, 2018 at 2:15
0

Determined to be another plugin that was causing the issue of the ordering, "Simple Custom Post Order" removed that plugin and ordereding was fixed but sub categories still didn't have unique classes. Added hierarchical=1 and that resolved that too!

4
  • My test shows unique classes for subcategories, without 'hierarchical' argument, which is true by default. Commented Dec 3, 2018 at 2:27
  • There are unique classes in regards to number classes, but mean there is no children class applied when not using hierarchical so was unable to add a css style to add indents for sub categories. All good now! Commented Dec 3, 2018 at 2:39
  • 'children' class is added to <ul> element, with or without 'hierarchical' argument. Commented Dec 3, 2018 at 2:44
  • That is not the case for me. If I don't add &hierarchical=1 it doesn't show class for whatever reason but does when I do. Hopefully this helps others and perhaps it's a bug where there is some use case where it's required. Commented Dec 5, 2018 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.