get_nav_menu_locations

Home » Functions » get_nav_menu_locations

Function Name: get_nav_menu_locations

In WordPress, the get_nav_menu_locations function is used to retrieve the registered navigation menu locations defined in your theme. This function returns an array that contains the location slugs as keys and the corresponding menu IDs as values.

Navigation menus are an essential element of any website as they provide a way for visitors to navigate through different sections or pages. WordPress allows you to create multiple menu locations in your theme, which can be customized with specific menus.

The get_nav_menu_locations function simplifies the process of retrieving the registered menu locations in your theme. By using this function, you can easily obtain the menu IDs assigned to each location.

Here’s an example usage code:

$menu_locations = get_nav_menu_locations();

// Accessing the primary menu ID $primary_menu_id = $menu_locations[‘primary-menu’];

// Accessing the footer menu ID $footer_menu_id = $menu_locations[‘footer-menu’];

In the above example, the get_nav_menu_locations function is called without any parameters, which retrieves all the registered menu locations. The returned array, $menu_locations, can then be used to access specific menu IDs by referencing their corresponding location slugs.

By utilizing the get_nav_menu_locations function, you can effortlessly retrieve the menu IDs associated with different menu locations in your WordPress theme, allowing you to customize the navigation menus as per your requirements.

Please note that this function will only work if your theme registers navigation menu locations using the register_nav_menus function.

So, next time you need to fetch the registered menu locations in your WordPress theme, remember to use the get_nav_menu_locations function for a streamlined development process.

Learn More on WordPress.org

WordPress snippets using the get_nav_menu_locations function

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