getButterfly Logo getButterfly code wrangling since 2005

Navigation menus in WordPress are not performing very well in case of lots of submenus, sections, multilevel items or the well-known mega-menus. Here’s a function that allows you to cache the output of your navigation menu.


  1. WordPress Lighthouse plugin
  2. A modern WordPress theme using wp_nav_menu() function


In order for this tutorial to work, activate the Lighthouse plugin and replace wp_nav_menu() with lhf_nav_menu() in your header.php and footer.php templates. All the other parameters stay the same. See sample usage below.

if(function_exists('lhf_nav_menu')) {
    lhf_nav_menu(array('theme_location' => 'primary', 'menu_class' => '', 'container' => 'div', 'container_class' => ''));
} else {
    wp_nav_menu(array('theme_location' => 'primary', 'menu_class' => '', 'container' => 'div', 'container_class' => ''));

Enjoy your faster menu!

Image credit

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

Leave a reply