Skip to main content
9 votes
Accepted

How to create this custom menu walker?

You need to set-up your own menu walker (link to Wordpress Codex), and in there add custom start_el and end_el overrides. So for example your start_el and end_el may look something like this: ...
Steven's user avatar
  • 608
6 votes
Accepted

Simple Navigation Walker – Wrapper-class around first sub-menu

To be honest, I don't really know why my solution works, but it does 😂 I based my snippet on this solution and tweaked the output to my needs: Custom nav walker with different output depending on ...
Jörg Mayer's user avatar
6 votes
Accepted

start_lvl on Walker is not working

Your menu doesn't appear to have any levels. start_lvl and end_lvl are used for the sub-menu wrappers. The outer wrapper for the menu, the <ul> is defined by the items_wrap argument of wp_nav_menu()...
Jacob Peattie's user avatar
5 votes
Accepted

Add Page ID class to nav menu items

We can add custom nav menu classes through the nav_menu_css_class filter. Example: The following should add the CSS class wpse-object-id-{object_id} to the <li> tags: // Add filter add_filter(...
birgire's user avatar
  • 67.4k
4 votes
Accepted

Pass a parameter to a menu walker

As @toscho said, you can call the walker class with parameters as you did: new BEMwalker( 'mobile' ) The constructor of BEMwalker will take the arguments (like any other function or method in PHP) ...
Sven's user avatar
  • 3,644
4 votes
Accepted

automatically placing custom post types singles into submenu of main nav menu

Walker_Nav_Menu isn't what you would use for this. Instead, use the wp_get_nav_menu_items filter. Before we start, you need some way of allowing the code to identify which is the menu item/page that ...
Dan.'s user avatar
  • 856
4 votes
Accepted

How to add parent element's title to nav menu's child?

I believe wp_nav_menu() handles the top-level list wrapping so start_lvl method would only handle submenu items ( from my understanding ). It also doesn't give us access to the $item which we need to ...
Howdy_McGee's user avatar
  • 20.7k
4 votes
Accepted

Using string instead of object class instantiation on the walker argument breaks wp_nav_menu

Apparently custom nav menu walkers are not supported though they were supposed to be. I'm surprised this only has been discovered now. However, there is an easy fix for you to get them to work. Just ...
Weston Ruter's user avatar
  • 4,030
4 votes
Accepted

Third level navigation class

You don't need to write your custom Walker for that... Let's take a look on built-in Walker_Nav_Menu. You'll find this function: public function start_lvl( &$output, $depth = 0, $args = array() )...
Krzysiek Dróżdż's user avatar
3 votes

Nav walker, bootstrap: Display 3rd level items under 2nd level

I solved this problem. follow these instructions..... Add script (function($){ $(document).ready(function(){ $('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) { ...
Md.Tauhidul Islam's user avatar
3 votes
Accepted

How to add attribute in menu code

I found one small and nice solution. add_filter( 'nav_menu_link_attributes', 'cfw_add_data_atts_to_nav', 10, 4 ); function cfw_add_data_atts_to_nav( $atts, $item, $args ) { $atts['data-hover'] = $...
pagol's user avatar
  • 191
3 votes

How to add attribute in menu code

If you want WordPress to add these items dynamically you would need to add a custom walker class to your menu declaration. Find where your menu is declared in the theme and add a custom walker class ...
Alex's user avatar
  • 31
3 votes

WordPress 4.4+ breaks Walker Extension

Your original solution was a hack, and no surprise it failed. In general never add methods/attributes to objects that you do not control their class and future development. The right way is to create ...
Mark Kaplun's user avatar
  • 23.4k
3 votes
Accepted

How to add a <span> tag to wp_nav_menu in a child theme

The wp_nav_menu() function has a ton ( or bunch ) of parameters which makes customization fairly easy. I believe the one you're specifically looking for is after which will add a passed string into ...
Howdy_McGee's user avatar
  • 20.7k
3 votes
Accepted

how to change Wordpress menu markup/classes

Changing the CSS is the simplest way to apply your desired styles. Another alternative, as Jack mentioned, is to create a custom walker. The Codex has a good overview. The third option is a happy ...
WebElaine's user avatar
  • 9,440
3 votes

How to actually make menu walker alive on admin interface?

You use the walker in your theme, wherever you call your nav menu. So for example, if you currently have a "topnav" menu in your theme, like this: wp_nav_menu(array('menu' => 'topnav'); and your ...
WebElaine's user avatar
  • 9,440
3 votes
Accepted

Adding a unique ID to each sub-menu

As far as I can know about menus in WordPress - handlers use menu arguments. You can try to use this. Using the argument filter, we can pass the ID of the item: add_filter( 'nav_menu_item_args', ...
vralle's user avatar
  • 201
2 votes

Get top items number from wordpress menu

You can use the wp_get_nav_menu_object to retrieve the menu as an object, which you can subsequently establish the length of. Like this: $wpse140949_menu = wp_get_nav_menu_object( 'your-menu-name-or-...
cjbj's user avatar
  • 14.6k
2 votes

Custom Nav Walker menu - Display children count

The cleanest approach to this solution would be to track the number of children from the display_element function (where the $this->has_children is set) which Walker_Nav_Menu uses from the original ...
AlexGW's user avatar
  • 21
2 votes

Custom Nav Walker menu - Display children count

I sort of added onto @Giri's answer by using array_map and array_count_values. if this helps anyone in the future. I didn't wish to use a counter and a foreach loop for something so simple. function ...
yaserso's user avatar
  • 397
2 votes

Add a custom walker to a menu created in a widget

You can add a custom walker to a nav menu created in a widget using the widget_nav_menu_args filter added in wordpress 4.2.0. It accepts four arguments (see wp-includes/widgets/class-wp-nav-menu-...
celleness's user avatar
2 votes

Add a custom walker to a menu created in a widget

This is an alternative to targeting a menu by term_id and I thought it might be of use to someone as a way of modifying several menus without having to look up their ids. By adding print_r($args) to ...
Jon's user avatar
  • 21
2 votes

Main Menu and Sub Menu Items for Footer

This is actually not that difficult if you already know how the walker class works. You would need two calls to wp_nav_menu. The first one you have should already work, because the function natively ...
cjbj's user avatar
  • 14.6k
2 votes

Adding post excerpt into navigation menu

wp_nav_menu() calls walk_nav_menu_tree() which calls the Walker_Nav_Menu class. There are a couple of filters you can hook into in those functions but I would take a look at the nav_menu_item_title ...
kingkool68's user avatar
  • 1,219
2 votes

Customize existing menu item

Given that you want to include a picture in the wp_nav_menu output, your best bet is to use the wp_nav_menu_items filter, as suggested in the comments. This will allow you to do a search and replace ...
cjbj's user avatar
  • 14.6k
2 votes
Accepted

Hooking to walker_nav_menu_start_el to insert list of subpages

I was able to solve my own problem by using get_the_title with the post ID rather than using the_title. Then I decided to try using $subpost->post_title and that worked too. I figure this is better ...
Josh's user avatar
  • 150
2 votes

Add custom text to menu

There is another alt way to do this with CSS3. You would add a link to the WP Menu as normal but make the Location either javascript:void(0); or # so it doesn't go anywhere (I would say the javascript....
Ray's user avatar
  • 230
2 votes

How can I add divs or change li in the submenu of the menu_nav

You don't need to make a whole new walker, you can do this with just a filter and some conditionals based on the function parameters. The filter we need is walker_nav_menu_start_el which you can see ...
ngearing's user avatar
  • 2,164
2 votes
Accepted

Output the aria-labelledby parameter for a nav menu child

I solved it. I thought it could work but i wasn't sure, and i wanted some opinion from more experienced people. Anyways, this is what i have done: First of all, i defined a private var in my walker ...
Daniele Squalo's user avatar
2 votes
Accepted

How to add a submenu toggle button inside all "li" elements that have a submenu?

I managed to solve this: Create the function So I created a new custom walker function in functions.php and copied all the code of the core wp nav walker function (wp-includes/class-walker-nav-menu....
Skovsgaard's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible