$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - No hierarchy in wp_list_categories with child of and depth|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - No hierarchy in wp_list_categories with child of and depth

matteradmin10PV0评论

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!

Post a comment

comment list (0)

  1. No comments so far