Remove a menu item in menu

I think the best way to remove a menu item from a menu is by hooking to wp_nav_menu_objects filter (@Otto answer from the thread you mentioned). And the way you use is first check for the correct menu to filter, and then search for the menu item and remove it by unsetting it from the … Read more

Add Class to Specific Link in Custom Menu

you can use nav_menu_css_class filter add_filter(‘nav_menu_css_class’ , ‘my_nav_special_class’ , 10 , 2); function my_nav_special_class($classes, $item){ if(your condition){ //example: you can check value of $item to decide something… $classes[] = ‘my_class’; } return $classes; } Using this $item you can any condition you want. and this will add the class to the specific li and you … Read more

Error “Trying to get property of non-object” with Custom Walker for wp_nav_menu

I get this error when there are no menus defined or no menus set for the location at Appearance->Menus. When that occurs wp_nav_menu uses a page walker fallback. The fallback (default) for wp_nav_menu is wp_walker_page which uses wp_page_menu which uses wp_list_pages which uses walk_page_tree which uses Walker_Page not Walker_Nav_Menu. And apparently the two walkers are … Read more

How to get current-menu-item title as variable?

This is possible by filtering wp_nav_menu_objects, which is the easiest place to check which item is the current menu item, because WordPress already added the classes for you. add_filter( ‘wp_nav_menu_objects’, ‘wpse16243_wp_nav_menu_objects’ ); function wpse16243_wp_nav_menu_objects( $sorted_menu_items ) { foreach ( $sorted_menu_items as $menu_item ) { if ( $menu_item->current ) { $GLOBALS[‘wpse16243_title’] = $menu_item->title; break; } } … Read more

wp_get_nav_menu_items() not working with slug

Since nobody has really explained why the function doesn’t work the way you thought it did, I shall take a stab at explaining it in detail as I just fell for the same trap as you did. This is where the docs aren’t really clear as to what the slug is referring to. Most people … Read more

Add container to nav_menu sub menu

Use a custom walker, extend the methods start_lvl() and end_lvl. Sample code, not tested: class WPSE_78121_Sublevel_Walker extends Walker_Nav_Menu { function start_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat(“\t”, $depth); $output .= “\n$indent<div class=”sub-menu-wrap”><ul class=”sub-menu”>\n”; } function end_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat(“\t”, $depth); … Read more