
Traverse Tree-Like Data Structures with WordPress Walker Class
- February 29, 2016
- Leave a comment
In WordPress, certain data objects have “tree-like” structures such as navigation menus, taxonomies, pages, etc. These can have parents, children and siblings like any other tree like structures. An abstract WordPress class “Walker” provides some methods to traverse these tree-like structures. It was provided by WordPress in its version 2.1. As this class was defined prior to PHP5, therefore, it does not use any explicit abstract keyword. As it is implicitly abstract (compatible with PHP4), so you can implement only those methods that are required in your extended walker class.
This class does not do anything (in terms of generating HTML) on its own, rather just walk through each node (element) of the tree. In order to make use of this class, it must be extended using any one of the following abstract methods so that you can generate HTML for these tree-like data objects.
Methods:
Below mentioned are the methods declared as abstract in walker class. You need to explicitly define the methods in your child classes.
1 |
start_lvl(&$output, $depth = 0, $args = array()): |
This method is generally used to add the opening HTML tag for a new branch. This method runs whenever a new branch is traversed in the tree structure.
1 |
end_lvt(&$output, $depth = 0, $args = array()): |
It is generally used to add the ending tag for a branch that is being traversed. This method runs whenever walker is reached to the ending branch.
1 |
start_el(&output, $depth = 0, $args = array(), $current_object_id = 0): |
This method is called whenever an individual tree element starts traversing. It is generally used to add a starting tag for a single tree item.
1 |
end_el(&$output, $depth = 0, $args = array()): |
This method is called whenever an individual tree element ends traversing. It is generally used to add an ending tag for a single tree item.
Note: The $output parameter is passed by reference in these functions, therefore, the changes made to this variable will be handled automatically in them (functions). Hence, there is no need to return or echo any output generated by these functions.
There are two ways for the general usage of walker class. It can either be used as a callback or you can call it for custom purposes:
Callback
This class is mostly used as a callback by theme authors to override the default behavior of the walker for wp_nav_menu, wp_list_pages and wp_list_categories, etc. Through this, you can change the output of your lists or menus as you want in your theme. Most of the time, you just need to extend the abstract methods (which ever needed) mentioned above.
Custom
It is also possible to call walker class manually. This approach is mostly used by plugin developers. In this scenario, you should initiate the walker class using its public methods walk() or paged_walk(). The custom tree-like structure such as breadcrumbs can be walked like this.
User Comments