0

I'm trying to create my own custom menu using the Walker class and in the process of trying to figure out what's going on, I'm attempting to reproduce what wp_nav_menu() does by default, so, I've got this:

class My_Admin_Walker extends Walker
{
    public $db_fields = array ('parent' => 'parent', 'id' => 'menu_id');
}

function my_admin(  ){
   //$list = wp_nav_menu( array( 'walker' => new My_Admin_Walker() ));//produces no output 
   //$list = wp_nav_menu();//outputs links to pages

   return $list;
}

 my_admin();

When I pass my own My_Admin_Walker class to wp_nav_menu it produces no output. So I think I need to set the required $db_fields to something. The Walker docs say:

The value of each key should be the names of the object properties that hold the parent id and item id, respectively.

But I'm stuck on figuring out what the $db_fields should be set to, to produce the default output or any other output. I've tried field names in the wp database but had no luck so far. Where do I look up these object properties it's talking about?

Thanks for any help!

0

If you want to build your own custom nav menu you should be extending the Walker_Nav_Menu class, not the Walker class.

When building a custom menu walker the $db_fields should be what the default Walker_Nav_Menu class has:

$db_fields = array ('parent' => 'menu_item_parent', 'id' => 'db_id');

When building the navigation link for a menu item, the parent field maps to the _menu_item_menu_item_parent post meta field if the menu item has a parent.

The db_id maps to the post ID of the menu item.

Check out wp_setup_nav_menu_item() for good reference to better understand what is happening. https://developer.wordpress.org/reference/functions/wp_setup_nav_menu_item/

  • Many thanks for your answer I shall check out those docs. Do you happen to know if the Walker_Nav_Menu can de used to customise the nav menu in the Admin interface? – Jelnet Apr 14 '16 at 16:48
  • The navigation menu in the sidebar of the admin? Walker_Nav_Menu isn't used to build that. The Walker_Nav_Menu class is used to build navigation menus based on what you set in Appearance -> Menus. This is because it uses the nav_menu_item post_type to build them. – darrinb Apr 14 '16 at 17:10
  • Thanks again. Do you know how I can alter the admin menu? In particular I'm wanting to add something to the html title attribute. – Jelnet Apr 14 '16 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.