How to display few menus using one function in WordPress?

Acclaim/Blog/Snippets/How to display few menus using one function in WordPress?
  • Comments icon
  • 3 minutes of reading

Are you looking for a way to display menus on your WordPress site? Then take a look at this! This tutorial will show you how to create a custom menu that displays in the footer of your site.

WordPress menu in footer

Many users want to display a menu on their WordPress site in the footer. When they use menu function on WordPress, they want to show a lot of options. It’s a common practice to display a few menus in a footer. Please see an example below:

Menu in the footer

Instead of creating each menu code separately, we can code it once, using a DRY methodology. Below, you can find an example of a function displaying all menus, no matter how many of them we have.

How to display few menus using for each loop?

First of all, we have to define menu localization properly. In Crunch we can do it in this file: inc/nav-menus.php. Using the function presented below we can define as many menus as we want to.

if(function_exists('register_nav_menus')) {
                    'main_navigation' => 'Main Navigation',
                    'footer_navigation_1' => 'Footer Navigation 1',
                    'footer_navigation_2' => 'Footer Navigation 2',
                    'footer_navigation_3' => 'Footer Navigation 3',
                    'footer_navigation_4' => 'Footer Navigation 4',

The keyword here is the footer which allows us to display only menus located in the footer.

In the next step, we have to paste the code presented below to our footer file.

$menus = get_registered_nav_menus(); // Get array with all menus
1. array_filter function allows us to choose menus with foooter location 
2. ARRAY_FILTER_USE_KEY parameter allows us to filter by array $menus key instead of value
$footer_menus = array_filter($menus, function($element){
    if(strpos($element, 'footer') !== false) {
        return $element;

Displaying our menus

The last step is displaying our menus.

<?php foreach($footer_menus as $key => $footer_menu): ?>

    <div class="">
         <h3><?= wp_get_nav_menu_name($key);?></h3>
                     'menu'            => $footer_menu,
                     'theme_location'  => $key,
                     'container_id'    => false,
                     'menu_id'         => false,
                     'depth'           => 2,

<?php endforeach; ?>

Related articles


Your email address will not be published. Required fields are marked *