How to add extra navigation item in existing WordPress menu

Adding an extra menu item in an existing navigation is quite interesting. If you want to add another menu item in your menu then we can do that by adding some codes in functions.php file. You can design that extra menu item differently from the main menu.

The following code will add an extra menu item to your existing menu but you need to read on those explanation what stands for what:

[php]
add_filter( 'wp_nav_menu_items', 'custom_menu_item', 10, 2 );

function custom_menu_item ( $items, $args ) {

if ($args->theme_location == 'main_menu') {

$myvariable = get_template_part('my-custom', 'item' );

$items .= '<li>' . $myvariable . '</li>';

}

return $items;

}
[/php]

The above filter will add an extra menu item but you need to change the menu location. In line number 5, theme_location should be changed to as per your menu name. You need to change the menu location name “main_menu” as per your theme.

In line number 7, $myvariable holds the content you will want to show into the menu item. You can add any type of content.

If you want, you can avoid using any variable. You can delete line number 7 and just insert your button content in in line number 9 inside the <li> tags. In this case, use the following codes:

[php]
add_filter( 'wp_nav_menu_items', 'custom_menu_item', 10, 2 );

function custom_menu_item ( $items, $args ) {

if ($args-&amp;gt;theme_location == 'main_menu') {

$items .= '<li> mymenuitem </li>';

}

return $items;

}
[/php]

Thus, you can add any number of extra menu item in your existing menu or navigation. Let me if you have any question.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top