Simple Example of implementing Walker_Nav_Menu Class

In reference to the WordPress Codex, the Walker class was implemented in WordPress 2.1 to provide developers with a means to traverse tree-like data structures for the purpose of rendering HTML.

The detail information about the walker class can be found in the WordPress Codex.

Here is a simple example.

In the above block of code, the wp_nav_menu() function takes $defaults as argument. In the array $defaults, the last key is walker. Te walker key’s value is object of a class Primary_Walker_Nav_Menu. In functions.php file implement the following code.

The start_el() method is used to add the opening HTML tag for a single tree item (such as , , or ) to $output.

 

The start_lvl() method is run when the walker reaches the start of a new “branch” in the tree structure. Generally, this method is used to add the opening tag of a container HTML element (such as , , or ) to $output.

The output of the above implementation will result in the following html block of code.

Published by karunshakya

Been programming in PHP / WordPress for over 4 years. I specialize in theme with Multilingual feature and plugin development and manipulation.