categories - No hierarchy in wp_list_categories with child of and depth

admin2025-06-05  1

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.

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.

Share Improve this question edited Dec 3, 2018 at 2:10 TechRemarker asked Dec 3, 2018 at 0:33 TechRemarkerTechRemarker 3932 gold badges6 silver badges21 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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

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!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749133445a316644.html

最新回复(0)