How to Add Custom Menu to certain Position in WordPress

I want to add custom menu to my primary menu, I have used this below code for this,

add_filter( 'wp_nav_menu_items', 'search_menu_item', 10, 2 );
function search_menu_item ( $items, $args ) {
if ($args->theme_location == 'secondary-menu') {
$items .= '<li class="border-none">SEARCH<form><input type="text" name="s" placeholder="Search Here" class="search-box"></form>';
}
return $items;
}

and menu is appearing as a last menu, but I want to add my menu to 3rd position. How do I do this

Read More

Can anyone Help??

Thanks

Related posts

2 comments

  1. You should rather use the wp_nav_menu_objects filter instead, which allows you to modify an array of items instead of a string.

    Example:

    add_filter( 'wp_nav_menu_objects', 'restructure_menu_links', 10, 2 );
    
    function restructure_menu_links( $items, $args ) {
    
        $new_links = array();
    
        $label = 'Lorem Ipsum';    // add your custom menu item content here
    
        // Create a nav_menu_item object
        $item = array(
            'title'            => $label,
            'menu_item_parent' => 0,
            'ID'               => 'yourItemID',
            'db_id'            => '',
            'url'              => $link,
            'classes'          => array( 'menu-item' )
        );
    
        $new_links[] = (object) $item; // Add the new menu item to our array
    
        // insert item
        $location = 3;   // insert at 3rd place
        array_splice( $items, $location, 0, $new_links );
    
        return $items;
    }
    
  2. Here are the steps to add custom menu in WordPress

    1. First of all, go to your admin-> appearance -> menus.
    2. Create a new menu using by clicking on the “+” sign as shown in the image.
    3. When you add a new menu, you can see the added menu in left “Primary Navigation”. Now you can add menu items from “custom links”, “Pages” and “Categories”.
    4. When you add menu items, you can see that item in the right side. You can see “Yahoo” in above image. Its an external link menu item. If you want to make any menu item as a child page of any other menu item, just drag that menu link and place below the parent. See above image. in Above image, “Uncategorized” is a child of “yahoo”.
    5. You can create as many menus as you want. But you can show menus as compatible with your theme. You can create as many menus as you want.
    6. You can call your menu wherever you want by just placing this one line code.

             <?php $defaults = array( 'theme_location'  => ,
             'menu'            => ,
             'container'       => 'div',
             'container_class' => 'menu-{menu slug}-container',
             'container_id'    => ,
             'menu_class'      => 'menu',
             'menu_id'         => ,
             'echo'            => true,
             'fallback_cb'     => 'wp_page_menu',
             'before'          => ,
             'after'           => ,
             'link_before'     => ,
             'link_after'      => ,
             'items_wrap'      => '
             <ul id="%1$s" class="%2$s">%3$s</ul>',
             'depth'           => 0,
             'walker'          => );
             ?>
             <?php wp_nav_menu( $args ); ?> 
      

    You can select arguments as per your need. Don’t forget to include the name of the menu in the “menu” argument.

Comments are closed.