How to add a custom menu to a specific location in Wordpress


Mayanke

I want to add a custom menu in the main menu, for this I have used this menu in the code below,

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 the menu is displayed as the last menu, but I want to add the menu to the 3rd position. What should I do

can anyone help?

thanks

phan

You should instead use the wp_nav_menu_objects filter, which allows you to modify an array of items instead of strings.

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;
}

Related


How to add a custom menu to a specific location in Wordpress

Mayanke I want to add a custom menu in the main menu, for this I have used this menu in the code below, add_filter( 'wp_nav_menu_items', 'search_menu_item', 10, 2 ); function search_menu_item ( $items, $args ) { if ($args->theme_location == 'secondary-menu') {

How to add a custom menu to a specific location in Wordpress

Mayanke I want to add a custom menu in the main menu, for this I have used this menu in the code below, add_filter( 'wp_nav_menu_items', 'search_menu_item', 10, 2 ); function search_menu_item ( $items, $args ) { if ($args->theme_location == 'secondary-menu') {

How to Add Custom Items to Specific WordPress Menu Item Locations

Kabu I signed up and was shown a main menu with 4 links (Home, About, News, Blog). I want to add html (logo) between the second menu and the third menu, I want to know if it is possible. Here's a chart: About | Logo | News | Blog I was looking at the hook wp_n

How to add location URL (Appearance -> Menu -> URL) in Wordpress?

Vilas Kumkar I'm working on a WP site that currently has a URL for this kind of test instance: http://someipaddress/~directory/directory/ followed by "wp-admin" Now if I want to link to any page in the top nav via wp-admin->appearance->menu I need to use "/~di

How to add location URL (Appearance -> Menu -> URL) in Wordpress?

Vilas Kumkar I'm working on a WP site that currently has a URL for this kind of test instance: http://someipaddress/~directory/directory/ followed by "wp-admin" Now if I want to link to any page in the top nav via wp-admin->appearance->menu I need to use "/~di

WordPress add element to 1 specific menu item

Chris I have a wordpress menu and I have a page in this menu and ProductsI want to add a small counter on this menu item. But I don't know how to edit that particular item. I counted the products I have, but am not sure where to echo it. <?php $countProducts =

WordPress add element to 1 specific menu item

Chris I have a wordpress menu and I have a page in this menu and ProductsI want to add a small counter on this menu item. But I don't know how to edit that particular item. I counted the products I have, but am not sure where to echo it. <?php $countProducts =

WordPress add element to 1 specific menu item

Chris I have a wordpress menu and I have a page in this menu and ProductsI want to add a small counter on this menu item. But I don't know how to edit that particular item. I counted the products I have, but am not sure where to echo it. <?php $countProducts =

WordPress add element to 1 specific menu item

Chris I have a wordpress menu and I have a page in this menu and ProductsI want to add a small counter on this menu item. But I don't know how to edit that particular item. I counted the products I have, but am not sure where to echo it. <?php $countProducts =

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

add class to ul in custom wordpress menu

User 2936461 I am trying to add a class to the default ul generated in a custom wordpress menu. I created a custom menu on the backend and set it up, after referencing the wordpress Codex, this is the code I'm currently using: <?php if ( has_nav_menu( 'main-n

add class to ul in custom wordpress menu

User 2936461 I am trying to add a class to the default ul generated in a custom wordpress menu. I've created a custom menu on the backend and set it up, after referencing the wordpress Codex, this is the code I'm currently using: <?php if ( has_nav_menu( 'mai

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link

Add custom data attribute to Wordpress navigation menu

gap I've seen similar questions on stackoverflow, but mine is a bit different. I need to be able to add custom data attributes to wordpress menus. The problem I'm having is all the solutions I've found, such as the following example: add_filter( 'nav_menu_link