Skip to main content
74 votes

Get WP Navigation Menu from REST API V2

Since I don't like it myself when the top answer is "Install plugin X", here is how I solved it: Menus are currently not available in the WP Rest. So what you need to do is register your own custom ...
Liren's user avatar
  • 841
63 votes

Retrieving a list of menu items in an array

As @vancoder mentions, wp_get_nav_menu_items() is the way to go, however I think a better detailed answer would be more helpful for people in the future. Said function returns an array of WP_Post ...
Brad Adams's user avatar
33 votes
Accepted

Adding a menu item for a Custom Post Archive

You can use a built in function of the menu editor for this. Go to your Custom Post Type in the Menu editor, click the "view all" tab and the first item you see is a link to the CPT archive page. In ...
Florian's user avatar
  • 1,575
30 votes

How do I get the name of a menu in WordPress?

On WordPress version 4.9.0 and above you can use wp_get_nav_menu_name($location) wp_nav_menu_name for more
Merhawi Fissehaye's user avatar
28 votes
Accepted

How to modify navigation menu of the "My Account" page in WooCommerce

For that, you do not need to modify the woocommerce/templates/myaccount/navigation.php. The best way to customize the "My Account" navigation menu items is to use: woocommerce_account_menu_items ...
ClemC's user avatar
  • 1,965
28 votes
Accepted

Apply the_title() filter in post & page title, but not in menu title

Problem Description: Let me rephrase the question first. You want to: Set new title to all post and page type from a meta field. You want this to happen everywhere (home page, single page, widgets ...
Fayaz's user avatar
  • 8,937
27 votes
Accepted

Add custom classes to anchor in wp_nav_menu

You can do this with the nav_menu_link_attributes filter. add_filter( 'nav_menu_link_attributes', 'wpse156165_menu_add_class', 10, 3 ); function wpse156165_menu_add_class( $atts, $item, $args ) { ...
cjbj's user avatar
  • 14.6k
24 votes
Accepted

wp_get_nav_menu_items() not working with slug

Since nobody has really explained why the function doesn't work the way you thought it did, I shall take a stab at explaining it in detail as I just fell for the same trap as you did. This is where ...
JoeMoe1984's user avatar
18 votes

Error: Declaration of MyClass::start_lvl() should be compatible with that of Walker_Nav_Menu::start_lvl()

From class Walker_Nav_Menu replace this lines function start_el(&$output, $category, $depth, $args) { to function start_el(&$output, $category, $depth = 0, $args = array(), $...
Mohammad Abedi's user avatar
16 votes

How to add a Custom Link to a Menu with a URL that is relative to the blog URL

I was also looking for a solution for this and I found simple one. This is what you need to place into the URL field: /index.php/internal-site-name It works just fine! Best Regards Chris
Christian Hatz's user avatar
14 votes

How to Add <span> to Each Menu Link with link text to data-attr?

Since version 4.4.0 the 'nav_menu_item_args' filter was added. This allows you to set 'link_before' and 'link_after' attributes for each item. add_filter('nav_menu_item_args', function ($args, $item, ...
Daren Zammit's user avatar
14 votes
Accepted

How do I get the name of a menu in WordPress?

You can access the menu metadata using the wp_get_nav_menu_object function BY NAME: $menu = wp_get_nav_menu_object("my mainmenu" ); BY SLUG: $menu = wp_get_nav_menu_object("my-mainmenu" ); The ...
Den Isahac's user avatar
14 votes

Adding a custom field to the site identity menu

You'll have to add your own customizer controls to achieve that. So for example, if you want to add Company Name, you can use this code: function my_register_additional_customizer_settings( $...
Krzysiek Dróżdż's user avatar
13 votes

How to limit wordpress menu depth in admin panel

The solution that I came up with: /** * Limit max menu depth in admin panel to 2 */ function q242068_limit_depth( $hook ) { if ( $hook != 'nav-menus.php' ) return; // override default value ...
jmarceli's user avatar
  • 1,066
12 votes

add_menu_page() with different name for first submenu item

Make the slug of the parent menu item and sub-menu same (first one item) as below function actions_recent_bids_add_admin_page(){ add_menu_page( 'Recent Bids', 'Auction Reports'...
Younas Ali's user avatar
10 votes

Using filters to change href of nav menu page link

You are on right track, with few minor kinks. You need to modify $atts and return it. Any arguments after the first one are provided for information and should not be changed. You need to tell ...
Rarst's user avatar
  • 99.7k
9 votes

Increase search results for Admin -> Appearance -> Menus -> Search (default is 10)

No need to change core files! Here is a hook (add in functions.php or simple plugin): // filtering quick-menu-search results (this seems better than others at https://pastebin.com/raw/jRkJYAzE ) ...
T.Todua's user avatar
  • 5,810
9 votes
Accepted

How to create this custom menu walker?

You need to set-up your own menu walker (link to Wordpress Codex), and in there add custom start_el and end_el overrides. So for example your start_el and end_el may look something like this: ...
Steven's user avatar
  • 608
8 votes

Retrieving a list of menu items in an array

Modified version of above with recursive loops. Add To Functions.php function wp_get_menu_array($current_menu='Main Menu') { $menu_array = wp_get_nav_menu_items($current_menu); $menu = ...
Pete's user avatar
  • 81
8 votes

Displaying Logged-In User Name in Wordpress Menu

The code: function give_profile_name($atts){ ... } gives a warning. Better is: function give_profile_name(){ ... } And it is better to detect if the user is logged in or not. Like so: function ...
Eutychus Mak's user avatar
8 votes

Get WP Navigation Menu from REST API V2

@Liren answer works well. However few beginners may not able to adjust the route. Here is the code that works well with WordPress Rest API v2 with minimum modification. Replace your menu name only in ...
Deepak Rajpal's user avatar
8 votes
Accepted

Removing link '<a>' from '<li>' menu for some "links" without JS

Assuming you're using wp_nav_menu() to display your navigation you could apply a walker that looks for css classes: $items_wrap = '<nav class="...">'; $items_wrap .= '<ul id="%1$s" class="%...
Howdy_McGee's user avatar
  • 20.7k
8 votes
Accepted

how to wrap <a> tag in <span> tag html to wordpress menu conversion

You'll want to use the link_before and link_after arguments for the wp_nav_menu. Setting them to '<span>' and '</span>' respectively. wp_nav_menu( [ 'menu_class' => 'menu menu-...
Nathan Johnson's user avatar
8 votes
Accepted

How can I hook into and edit the text of a wp_nav_menu <a> tag?

One way to achieve such effect would be to use wp_nav_menu_objects hook. function modify_home_in_nav_menu_objects( $items, $args ) { foreach ( $items as $k => $object ) { // you can ...
Krzysiek Dróżdż's user avatar
7 votes

How to add menu support to a theme?

Put this pretty much anywhere (ie. functions.php) add_theme_support( 'menus' );
Joel M's user avatar
  • 427
7 votes

How do I get the name of a menu in WordPress?

You can get the name like this, using the menu location, so if the menu is updated or you assign other menu you dont have to update anything here: $locations = get_nav_menu_locations(); //get all ...
David Lee's user avatar
  • 3,891
7 votes
Accepted

renaming an admin menu item with decimal array index number

You have to use a string as key: $menu['26.0648'][0] = 'Event Calendar'; If you write the key as a number, it will truncate the decimal to an integer, so 26.0648 will be truncated to 26.
ahendwh2's user avatar
  • 696
7 votes

Adding a custom field to the site identity menu

This is all part of the Customizer API; you'll create a section, add a setting to the section, then add a controller to the setting. Once complete you'll show it on your theme using get_theme_mod() ...
David Sword's user avatar
  • 3,337
7 votes
Accepted

Add settings to menu items in the Customizer

You're running up against an incomplete implementation of modifying nav menus in the Customizer. In particular, inside of the phpdoc for WP_Customize_Nav_Menu_Item_Setting::preview() you can see this ...
Weston Ruter's user avatar
  • 4,030
6 votes

How to add a Custom Link to a Menu with a URL that is relative to the blog URL

You can use the nav_menu_link_attributes filter to inspect and modify the href attribute for each menu item before it is output. In this example, we look for any href attributes that start with a /, ...
Milo's user avatar
  • 78.5k

Only top scored, non community-wiki answers of a minimum length are eligible