最新消息: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)

customization - Reproducing hierarchical list output from wp_list_categories(), using get_categories()

matteradmin4PV0评论

I need more control over the category listing output, so I'm using get_categories(), instead of wp_list_categories().

This function returns a flat array of objects ordered by a certain attribute. How can I build a hierarchical list from it like wp_list_categories() does?

I need more control over the category listing output, so I'm using get_categories(), instead of wp_list_categories().

This function returns a flat array of objects ordered by a certain attribute. How can I build a hierarchical list from it like wp_list_categories() does?

Share Improve this question edited Apr 5, 2019 at 21:48 Sven 3,6841 gold badge35 silver badges48 bronze badges asked Nov 19, 2010 at 22:53 AlexAlex 1,0811 gold badge15 silver badges25 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

The most ideal solution for walking over the data is to use the WordPress walker class.

http://codex.wordpress/Function_Reference/Walker_Class

You won't find many an example around the web for using it, but one was given by Scribu here.

http://scribu/wordpress/extending-the-category-walker.html

You can also look to the classes WordPress uses to extend the walker as further examples.

http://core.trac.wordpress/browser/tags/3.0.1/wp-includes/classes.php

Hope that helps..

How to build a hierarchical tree from get_categories

See this Stack Overflow question. In the answer Jan Fabry does it like this:

$categories = get_categories();

// First index all categories by parent id, for easy lookup later
$cats_by_parent = array();
foreach ($categories as $cat)
{
  $parent_id = $cat->category_parent;
  if (!array_key_exists($parent_id, $cats_by_parent)) 
  {
    $cats_by_parent[$parent_id] = array();
  }
  $cats_by_parent[$parent_id][] = $cat;
}

// Then build a hierarchical tree
$cat_tree = array();
function add_cats_to_bag(&$child_bag, &$children)
{
   global $cats_by_parent;
   foreach ($children as $child_cat) 
   {
     $child_id = $child_cat->cat_ID;
     if (array_key_exists($child_id, $cats_by_parent)) 
     {
        $child_cat->children = array();
        add_cats_to_bag($child_cat->children, $cats_by_parent[$child_id]);
     }
     $child_bag[$child_id] = $child_cat;
   }
}
add_cats_to_bag($cat_tree, $cats_by_parent[0]);
Post a comment

comment list (0)

  1. No comments so far