[wp-trac] Re: [WordPress Trac] #2593: The walk_tree family should be a class

WordPress Trac wp-trac at lists.automattic.com
Sat Mar 25 15:49:31 GMT 2006

#2593: The walk_tree family should be a class
       Id:  2593            |      Status:  new                     
Component:  Administration  |    Modified:  Sat Mar 25 15:49:31 2006
 Severity:  normal          |   Milestone:                          
 Priority:  normal          |     Version:  1.2                     
    Owner:  anonymous       |    Reporter:  davidhouse              
Comment (by davidhouse):

 That was a crap explanation. Lets go again.

 {{{walk_tree()}}} is a nice new generic function that got added to be the
 core a few weeks ago. It's a kind of generic pretty-printer for tree-like
 structures. For example, we used to have {{{list_cats()}}} and
 {{{wp_list_pages()}}}, both of which printed a hierarchial structure out
 (the category list for the former, the page list for the latter).
 {{{walk_tree()}}} generalises this by allowing you to pass in some
 callbacks that will get called when starting a new element, finishing an
 element, or ascending or descending a level.

 Callbacks are ugly when abused like this, so this is how my class does it:
 suppose you want to print some hierarchial tree. Create a new class which
 extends {{{Walker}}} and defines the following methods:

  * {{{start_el()}}}
  * {{{end_el()}}}
  * {{{start_lvl()}}}
  * {{{end_lvl()}}}

 The names should be fairly self-explanatory. You'll also need to define
 $dbfields, an associative array:

  * {{{$dbfields['parent']}}} should be the DB field which is the parent of
 any given record. This is {{{post_parent}}} for posts and
 {{{category_parent}}} for cats.
  * {{{$dbfields['id']}}} is the same but for the ID. E.g. {{{ID}}} for
 posts and {{{cat_ID}}} for cats.

 Once that's done, create an instance and call {{{$instance->walk()}}}
 passing in the elements you want to print (in object form), and the
 maximum depth (or -1 to just print flat, no hierarchy). Any extra
 parameters will get passed to your callbacks.

 There are examples in the code.

Ticket URL: <http://trac.wordpress.org/ticket/2593>
WordPress Trac <http://wordpress.org/>
WordPress blogging software

More information about the wp-trac mailing list