14

In my theme there is a function for nav menus

function ct_nav() {
  <nav>
     <?php wp_nav_menu( array( 'container_id' => 'nav', 'theme_location' => 'primary') ); ?>
  </nav>
}

How could i add more item manually? using this function alone.

2
  • Why don't you just add the markup yourself after the function call? Commented Sep 11, 2012 at 2:56
  • Yeah I don't understand what you mean, you can manually add items within the markup you have above? What are you trying to add exactly? Commented Sep 11, 2012 at 4:11

4 Answers 4

29
function add_last_nav_item($items) {
  return $items .= '<li><a href="#myModal" role="button" data-toggle="modal">Contact</a></li>';
}
add_filter('wp_nav_menu_items','add_last_nav_item');
Sign up to request clarification or add additional context in comments.

6 Comments

Can someone help me how to appear same on mobile menu only? thanks in advance
Hi, Can you explain more what you want to appear in mobile ?
@PreethamHegde Generally If you want to get something appear in mobile in wordpress you can use : <?php if ( wp_is_mobile() ) { /* Include/display resources targeted to phones/tablets here / } else { / Include/display resources targeted to laptops/desktops here */ } ?>
@SaidErraoudy with your example, how can we target a specific item by its title? rather than by the entire html line? I would like to setup a condition inside of your function.
For those who getting following error Uncaught ArgumentCountError. Extend the parameter of add_filter with 10 and 2. So it'll look like this. add_filter('wp_nav_menu_items','add_last_nav_item', 10, 2);
|
27

here is an example by changing the items_wrap.

wp_nav_menu( array( 'items_wrap' => '<ul id="%1$s" class="%2$s"><li><a href="http://www.google.com">go to google</a></li>%3$s</ul>' ) );

just took the default value and added the href.

2 Comments

Wow that's a dirty hack. Works, though 🙈
@Edward sadly, that is not a hack, neither is Said Erraoudy soultion, it's providing a template - nothing hacky about that, WP just chose here a very, VERY unfortunate way of how to do that - this part and whole nav-menu.php is a very "legacy" code of WP...
4

Just for the case someone needs this:

Menu items can be added manually by applying filters:

  • wp_nav_menu_items - for all menus
  • wp_nav_menu_{$menu->slug}_items - for menu with particular slug

Also by changing items_wrap, e.g., by removing <ul> and adding it explicitly in the theme - this way you will be able to add own items.

1 Comment

This requires a menu to already be assigned to the menu_location. Is there a way to do this without a menu being assigned (in Appearances > Menus). This would be helpful for multisite work I am doing.
1

None of the above answers worked for me. This is a jquery type of workaround I used. I needed to add an image to the end of my menu.

Use wp_nav_menu() as per normal, be sure to specify a class in menu_class or you can specify an ID.

$items = array(
    'theme_location'  => 'header-menu',
    'menu'            => '',
    'container'       => 'div',
    'container_class' => 'menu-{menu slug}-container',
    'container_id'    => '',
    'menu_class'      => 'menuContainer', /* important, since we're targetting it with jquery*/
    'menu_id'         => '',
    'echo'            => true,
    'fallback_cb'     => 'wp_page_menu',
    'before'          => '',
    'after'           => '',
    'link_before'     => '',
    'link_after'      => '',
    'depth'           => 0,
    'walker'          => ''
);
wp_nav_menu($items);

$( document ).ready(function() {
            $(".menuContainer ul").append("<li><img src='<?php echo  get_template_directory_uri(); ?>/img/menuImage.png'></li>");
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.