Add custom menu item to the primary nav in wordpress?

Hello,

I am trying to figure out how to add a custom menu item to the primary nav bar.

I am basically trying to incorporate a mega menu type thing using http://geedmo.github.io/yamm/, a mega menu built for bootstrap. I have it working, but I now need to be able to add the custom menu item to the primary nav - ideally in the last position of the

    produced by wordpress and the roots nav walker.

    I found some code and tried putting it in the nav.php file, but it just threw up an error, I’m not massively php savvy:

function add_last_nav_item($items) {
  return $items .= '<li><a href="#myModal" role="button" data-toggle="modal">Contact</a></li>';
}
add_filter('roots_nav_menu_items','add_last_nav_item');

I changed the above bottom line from wp_nav_menu_items, but that didn’t work either.

Any pointers on this would be amazing. I’m sure there must be a relatively simple way of doing it, but I can’t find anything after hours of searching google.

If more info is required, or I need to explain better. I’m happy to do it!

Many thanks,

Ali.

1 Like

You need to tell add filter how many arguments to expect. Also, instead of making up filters, you’re better off searching the codex for an explanation of the code.

function add_last_nav_item($items, $args) {
  if (!is_admin() && $args->theme_location == 'primary_navigation') {
    $items .= '<li><a href="#myModal" role="button" data-toggle="modal">Contact</a></li>';
  }
  return $items;
}
add_filter( 'wp_nav_menu_items', 'add_last_nav_item', 10, 2 );
2 Likes

Hi guys,

Using Foxaii’s example I was able to add a list of authors to the main menu (working fine), but once I tried to do the same with categories, they get listed outside the main ul. Here’s the working code I’m using for Authors, and for Categories.
Any idea why the second one doesn’t echo the list inside the dropdown ul?

function add_last_nav_item($items, $args) {
  if (!is_admin() && $args->theme_location == 'primary_navigation') {
    $items .= '<li><a class="dropdown-toggle" data-toggle="dropdown" data-target="#" href="#">Autoren <b class="caret"></b></a><ul class="dropdown-menu"><li>' . wp_list_authors('show_fullname=1&optioncount=0&exclude_admin=0&orderby=post_count&order=DESC&number=8&echo=0') . '</li></ul></li>';
  }

  return $items;
}
add_filter( 'wp_nav_menu_items', 'add_last_nav_item', 10, 2 );

And the categories:

function add_last_nav_item_categories($items, $args) {
  if (!is_admin() && $args->theme_location == 'primary_navigation') {
    $items .= '<li><a class="dropdown-toggle" data-toggle="dropdown" data-target="#" href="#">Kategorien <b class="caret"></b></a><ul class="dropdown-menu"><li>' . wp_list_categories('show_count=0&exclude=3&depth=-1&title_li=&use_desc_for_title=0&child_of=7') . '</li></ul></li>';
  }

  return $items;
}
add_filter( 'wp_nav_menu_items', 'add_last_nav_item_categories', 10, 2 );

http://discourse.roots.io/t/automatically-listing-authors-and-categories-in-custom-menu/1060/2

1 Like

When adding this code exactly as is to extras.php I get the following error:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘add_last_nav_item’ not found or invalid function name in /Users/mike/Sites/Development/lendwise.local/html/web/wp/wp-includes/plugin.php on line 235

1 Like

You’re exactly right. Thank you!