Using WordPress ‘get_registered_nav_menus()’ PHP function

The get_registered_nav_menus() WordPress PHP function retrieves all registered navigation menu locations in the active theme.

Usage

$menus = get_registered_nav_menus();

Parameters

  • None

More information

See WordPress Developer Resources: get_registered_nav_menus()

Examples

Display a simple list of the registered menus

This example retrieves all registered navigation menus and displays them as a list.

$menus = get_registered_nav_menus();
foreach ($menus as $location => $description) {
    echo $location . ': ' . $description . '<br />';
}

Display registered menus in a select dropdown

This example retrieves all registered navigation menus and creates a select dropdown for choosing one.

echo '<select>';
$menus = get_registered_nav_menus();
foreach ($menus as $location => $description) {
    echo '<option value="' . $location . '">' . $description . '</option>';
}
echo '</select>';

Check if a specific menu location is registered

This example checks if a specific menu location, ‘primary-menu’, is registered in the theme.

$menus = get_registered_nav_menus();
if (array_key_exists('primary-menu', $menus)) {
    echo 'Primary menu is registered.';
} else {
    echo 'Primary menu is not registered.';
}

Count the number of registered menus

This example retrieves all registered navigation menus and counts them.

$menus = get_registered_nav_menus();
$menu_count = count($menus);
echo 'There are ' . $menu_count . ' registered menus.';

Retrieve and display menu descriptions

This example retrieves all registered navigation menus and displays their descriptions in a list.

$menus = get_registered_nav_menus();
foreach ($menus as $description) {
    echo $description . '<br />';
}