WordPress. Adding menu item manually in wp_nav_menu

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.

Related posts

Leave a Reply

4 comments

  1. 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');
    
  2. 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.

  3. 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.

  4. 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>");
            });