Custom menu with Walker class - what should the $db_fields be?

admin2025-05-31  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!

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!

Share Improve this question asked Apr 14, 2016 at 13:25 JelnetJelnet 312 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/reference/functions/wp_setup_nav_menu_item/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748632980a313668.html

最新回复(0)