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

Add settings to menu items in the Customizer

You’re running up against an incomplete implementation of modifying nav menus in the Customizer. In particular, inside of the phpdoc for WP_Customize_Nav_Menu_Item_Setting::preview() you can see this comment: // @todo Add get_post_metadata filters for plugins to add their data. Nevertheless, even though we didn’t implemented core support for previewing changes to nav menu item postmeta, it … Read more

Using a menu walker add a custom item at the end of the menu’s items

You don’t need a walker in this case. A filter called wp_nav_menu_items is available. It allows you to edit the list items of a menu. Just append your own list item with search field. add_filter( ‘wp_nav_menu_items’, ‘add_search_to_nav’, 10, 2 ); function add_search_to_nav( $items, $args ) { $items .= ‘<li>SEARCH</li>’; return $items; } Note: if you … Read more

Add a custom walker to a menu created in a widget

If you look at implementation of WP_Nav_Menu_Widget class you will see the following code: function widget($args, $instance) { // Get menu $nav_menu = ! empty( $instance[‘nav_menu’] ) ? wp_get_nav_menu_object( $instance[‘nav_menu’] ) : false; if ( !$nav_menu ) return; $instance[‘title’] = apply_filters( ‘widget_title’, empty( $instance[‘title’] ) ? ” : $instance[‘title’], $instance, $this->id_base ); echo $args[‘before_widget’]; if … Read more

Get page IDs from nav items

Menu items are stored in the posts table with a post_type of nav_menu_item. So, what you are returning is the ID of the menu item itself, not what it points to. The page/post ID that the menu item refers to is stored in the postmeta table, with a post_id that matches the menu item ID … Read more

Convert output of nav_menu items into a tree-like multidimensional array

The problem of building a tree from a flat array has been solved here with this, slightly modified, recursive solution: /** * Modification of “Build a tree from a flat array in PHP” * * Authors: @DSkinner, @ImmortalFirefly and @SteveEdson * * @link https://stackoverflow.com/a/28429487/2078474 */ function buildTree( array &$elements, $parentId = 0 ) { $branch … Read more