Different Menus for multiple Pages
-
Hello, looking for a DRY code example to serve different menus for specific pages using a single navigation template. No plugins please and I am relatively new to PHP.
The 3 menus are registered: primary, auto, motorcycle
In site-nav.php:
<?php if ( is_front_page()) : wp_nav_menu([ 'theme_location' => 'primary', 'menu_class' => 'navbar-nav nav' 'container_class' => 'navbar navbar-s-page' 'container_id' => 'navbar-s-page' ]); elsif ( is_page(3)) : wp_nav_menu([ 'theme_location' => 'auto', 'menu_class' => 'navbar-nav nav' 'container_class' => 'navbar navbar-s-page' 'container_id' => 'navbar-s-page' ]); elsif ( is_page( array[1, 4])) : wp_nav_menu([ 'theme_location' => 'motorcycle', 'menu_class' => 'navbar-nav nav' 'container_class' => 'navbar navbar-s-page' 'container_id' => 'navbar-s-page' ]); endif; ?>
I would like to avoid repeating this code, I could write 3 variables and set them to the conditional tags in place of the ‘theme_location’ value. Is there a DRYer solution using a condition? The ternary operator looks promising, then nesting may be required?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Different Menus for multiple Pages’ is closed to new replies.