Home » Functions » wp_get_nav_menu_items

WordPress comes with an excellent menu management system that allows you to create and manage your site’s menus with ease. The wp_get_nav_menu_items function is used to retrieve the items from a menu.

This function can be useful if you need to customize how your menu items are displayed on your website. For example, you can use this function to add classes or IDs to menu items, change the order of menu items, or even create custom menus for certain sections of your website.

The wp_get_nav_menu_items function accepts a single parameter, which is the ID or name of the menu you want to retrieve items from. The function returns an array of objects, where each object represents a menu item.

Here’s an example usage code for wp_get_nav_menu_items:

$menu_items = wp_get_nav_menu_items( 'main-menu' );

foreach ( $menu_items as $menu_item ) {
    echo '<a href="' . $menu_item->url . '">' . $menu_item->title . '</a>';

In this example, we’re using wp_get_nav_menu_items to retrieve all the items from the "main-menu" menu. Then, we’re looping through each menu item and outputting a link to the menu item’s URL with the title as the anchor text.

Overall, the wp_get_nav_menu_items function is a powerful tool that allows you to customize how your site’s menus are displayed.

Learn More on WordPress.org

WordPress snippets using the wp_get_nav_menu_items function

Register an account to save your snippets or go Pro to get more features.