Traverse Tree-Like Data Structures with WordPress Walker Class

In WordPress, certain data objects have “tree-likestructures 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.


Below mentioned are the methods declared as abstract in walker class. You need to explicitly define the methods in your child classes.

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.

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.

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.

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:


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.


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

Leave a Reply

Your email address will not be published. Required fields are marked *

    Get in Touch