Function Reference/Walker Class
Role of Walker
The walker class encapsulates the basic functionality necessary to output HTML representing WordPress objects with a tree structure. For instance pages and categories are the two types of objects that the WordPress 2.2 code uses the walker class to enumerate. While one could always display categories as you wished by using right func and looping through them using it takes a great deal of work to arrange subcategories below their parents with proper formatting. The walker class takes care of most of this work for you.
Methods and Properties
Note that the properties of the Walker class are intended to be set by the extending class and probably should not vary over the lifetime of an instance.
Also the method definitions of start_el, end_el, start_lvl, end_lvl only list one argument but are called using call_user_func_array and it is these arguments that are listed.
Properties
- $tree_type
- The classes extending Walker in the WordPress 2.2 distribution set this either to 'category' or 'page'. The code makes no use of this value.
- $db_fields
- An array with keys: parent and id. The value for these keys should be the name of the property in the objects walker will be applied to holding the id of the current object and parent object respectively.
Example
class Walker_Page extends Walker {
var $tree_type = 'page';
var $db_fields = array ('parent' =>
'post_parent', 'id' => 'ID');
Thus the Walker_Page class (part of wordpress 2.2) expects that if page is a page object then page->post_parent will give the id of that page's parent and page->ID will give the id of that page.
Methods
- walk($elements, $to_depth)
- Takes an array of elements ordered so that children occur below their parents and an integer $to_depth. A non-zero $to_depth caps the maximum depth to which the method will descend. If $to_depth is -1 the array is processed as if it was flat (no element is the child of another). Any additional arguments passed to walk will be passed unchanged to the other methods walk calls.
walk steps through the array $elements one by one. Each time an element is the child of the prior element walk calls start_lvl. Every time an element is processed walk calls start_el and then end_el. Each time an element is no longer below one of the current parents walk calls end_lvl.
- start_el($output, $element, $depth, \[optional args\])
- Classes extending Walker should define this function to return $output concatenated with the markup starting an element.
- end_el($output, $element, $depth, \[optional args\])
- Classes extending Walker should define this function to return $output concatenated with the markup ending an element. Note that elements are not ended until after all of their children have been added.
- start_lvl($output, $depth, \[optional args\])
- Classes extending Walker should define this function to return $output concatenated with the markup that should precede any of the child elements. For instance this often outputs a ul or ol tag.
- end_lvl($output, $depth, \[optional args\])
- Classes extending Walker should define this function to return $output concatenated with the markup that should end any of the child elements. For instance this often ends a ul or ol tag.
Examples
See Function_Reference/Walker_Page, Function_Reference/Walker_PageDropDown, Function_Reference/Walker_Category, Function_Reference/Walker_CategoryDropdown