I have some code that adds a menu item to a menu using wp_nav_menu_{$menu->slug}_items.

It's pretty easy to add a menu item as the first or last item of the menu, but I was wondering if there's any way to assign an order to the item, i.e. place it as the 3rd item in the menu.

If there's no method for that built into the filter, is there another way to achieve the same result?

  • The correct official way is to write your own Walker Class extension. Google for wp_nav_menu_walker on YT. Nov 13, 2019 at 17:13

1 Answer 1


you can always search & replace by string or regex the output html of the filtered $items. anyway, if you're targeting usual menu items, you can add $post objects and order items through the wp_nav_menu_objects filter, otherwise here's the basic sample code:

function replace_itemcart_in_menu( $items, $args ) { 
    $url = home_url('my-item');
    $previtem = '<li id="menu-item-11" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-11"><a href="'.$carturl.'/">My Item</a>'; 
    $newitem = '<li>New Item</li>'; 
    $items = str_replace($previtem, $previtem.$newitem, $items); 
    return $items; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.