可能不是你想要的,但你可以看看Bootstrap Nav Walker implementation.
在documentation, 还有一个例子manually calling your own walker 类,如果您还没有这样做的话,可能值得一读。
class Walker_Quickstart_Menu extends Walker {
// Tell Walker where to inherit it\'s parent and id values
var $db_fields = array(
\'parent\' => \'menu_item_parent\',
\'id\' => \'db_id\'
);
/**
* At the start of each element, output a <li> and <a> tag structure.
*
* Note: Menu objects include url and title properties, so we will use those.
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= sprintf( "\\n<li><a href=\'%s\'%s>%s</a></li>\\n",
$item->url,
( $item->object_id === get_the_ID() ) ? \' class="current"\' : \'\',
$item->title
);
}
}
然后打电话给步行者:
// 1. Fetch the menu (we\'ll assume it has an id of 2)...
$menu = wp_get_nav_menu_object(2);
// 2. Create an empty $menu_items array
$menu_items = array();
// 3. Get menu objects (this is our tree structure)
if ( $menu && ! is_wp_error($menu) && empty($menu_items) ) {
$menu_items = wp_get_nav_menu_items( $menu );
}
// 4. Create a new instance of our walker...
$walk = new Walker_Quickstart_Menu();
// 5. Walk the tree and render the returned output as a one-dimensional array
print_r( $walk->walk( $menu_items, -1 ) );