Milan 2 weeks ago

PHP navigation menus loop

So I am practicing PHP and making one wordpress theme, and I have some links in the footer which are 3 sepearte navigation menus. I have them created in the Dashboard, registered in the functions.php file and called in the footer.php file and everything works great.

This is how I called the menus in the footer.php So I repeated this code 3 times, for each menu. (Footer, Footer1 and Footer2)

<?php
    wp_nav_menu(array(

       'menu'               => 'Footer',
       'theme_location'     => 'footer',
       'container'          => 'nav',
       'container_class'    => 'footer-links-content',
       'menu_class'         => 'footer_list'
                    ));
    ?>

So, is there any better way I can call this 3 menus, with lesss code? Maybe some foreach loop?

Thanks!

Answers


Mike Nickaloff 2 weeks ago

Make an array of the footer values you want, then use foreach and substitute the name with the iterator variable..

<?php
  $footerList = array("Footer", "Footer1", "Footer2");

 foreach ($footerList as $idx=>$footer) { 
    wp_nav_menu(array(

      'menu'               => $footer,
      'theme_location'     => 'footer',
      'container'          => 'nav',
      'container_class'    => 'footer-links-content',
      'menu_class'         => 'footer_list'
   ));
 }
?>

Post Status

Asked in 2 weeks ago
Viewed 4,000 times
Voted 7
Answered 1 times

Search




Leave an answer