How Do I Create a Hamburger Menu in WordPress?
Creating a hamburger menu in WordPress is easy. Once you have created your menu, you will need to add a few lines of code to your WordPress theme’s functions.
php file.
To add a hamburger menu to your WordPress site, you will first need to create a menu item. To do this, add the following code to your theme’s functions.php file:
function my_menu_item() { $menu_item = new menu_item(); $menu_item->name = “My Hamburger Menu”; $menu_item->description = “This is a menu item for my hamburger menu.”; $menu_item->taxonomy = “menu_item”; $menu_item->parent = “main_menu”; $menu_item->position = 2; $menu_item->menu_id = “3”; $menu_item->add_submenu_item( $menu_item->name, $menu_item->description, “sub-menu-item”, “sub-menu-item-1″, “sub-menu-item-2″); return $menu_item; }
In this code, we have created a menu item named “My Hamburger Menu” and assigned it a description. We have also assigned it a taxonomy of “menu_item”, and it is the second item in the main menu.
We have also set its position to be number two, and assigned it an menu_id of “3”. Finally, we have added two submenu items to the menu item.
To add this menu item to your WordPress site, you will first need to add a line to your theme’s header.php file:
Next, you will need to add a line to your theme’s functions.php file, just before the closing ?> tag:
function my_menu() { $menu = my_menu_item(); $menu->name = “My Hamburger Menu”; $menu->description = “This is a menu item for my hamburger menu.”; $menu->taxonomy = “menu_item”; $menu->parent = “main_menu”; $menu->position = 2; $menu->menu_id = “3”; $menu->add_submenu_item( $menu->name, $menu->description, “sub-menu-item”, “sub-menu-item-1″, “sub-menu-item-2″); return $menu; }
Now, you can add a line to your theme’s header.php file to include the newly created menu:
My Hamburger Menu
And that’s it! Your hamburger menu is now up and running.