Performance: Menus and navigation
Menus and navigation impact page load speed and interaction responsiveness by affecting DOM size and script execution during user navigation.
Jump into concepts and practice - no test required
<?php wp_nav_menu(array('theme_location' => 'primary', 'depth' => 2)); ?>
<?php wp_nav_menu(array('theme_location' => 'primary', 'depth' => 0)); ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unlimited nested menu | High DOM nodes | Multiple reflows on open/close | High paint cost | [X] Bad |
| Limited depth menu | Moderate DOM nodes | Single reflow on toggle | Moderate paint cost | [OK] Good |
| Unconditional heavy script load | N/A | Blocks rendering | Blocks paint | [X] Bad |
| Conditional script loading | N/A | Non-blocking | Faster paint | [OK] Good |
| jQuery slideToggle animation | N/A | Multiple reflows per frame | High paint cost | [X] Bad |
| CSS transform opacity animation | N/A | No reflows | Low paint cost | [OK] Good |
wp_nav_menu() is used to show menus in themes.array('theme_location' => 'primary') tells WordPress which menu to show.wp_nav_menu(array('theme_location' => 'footer'));register_nav_menu('header-menu', 'Header Menu');after_setup_theme.register_nav_menu() directly in functions.php without hook may run too early, so WordPress doesn't register it properly.register_nav_menus() with an array of locations inside a function hooked to after_setup_theme.wp_nav_menu() with theme_location keys matching registered locations.function register_my_menus() {
register_nav_menus(array(
'header' => 'Header Menu',
'footer' => 'Footer Menu'
));
}
add_action('after_setup_theme', 'register_my_menus');
// In header.php
wp_nav_menu(array('theme_location' => 'header'));
// In footer.php
wp_nav_menu(array('theme_location' => 'footer')); correctly hooks registration to after_setup_theme, uses register_nav_menus(), and displays menus properly.