• Resolved fromthe80s

    (@fromthe80s)


    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?

    • This topic was modified 2 years, 11 months ago by fromthe80s.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter fromthe80s

    (@fromthe80s)

    Actually this is what I what to achieve with the menu navs rather.

    In site-nav.php:

    <?php
      if ( is_page(3)) :
        wp_nav_menu([
          'theme_location'  => 'auto',
          'menu_class'      => 'navbar-nav nav'
          'container_class' => 'navbar 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'
        ]);
      else :
        wp_nav_menu([
          'theme_location'  => 'primary',
          'menu_class'      => 'navbar-nav nav'
          'container_class' => 'navbar navbar-s-page'
        ]);
      endif;
    ?>
    MK

    (@mkarimzada)

    I think a better approach would be creating different page templates and headers. It keeps your theme/code clean, organized and it will be a lot easier for other developers to extend your theme.

    Hardcoding page IDs in the theme will be a problem once the site grew in content.

    Here is another approach if you decided to hardcode the page IDs:

    $menus = array(
        'primary' => '',
        'motorcycle' => array(1, 4),
        'auto' => '3'
    );
    
    foreach( $menus as $key => $page_id ) {
        if( is_numeric($page_id) || is_array($page_id) && is_page($page_id)  ) {
            wp_nav_menu([
              'theme_location'  => $key,
              'menu_class'      => 'navbar-nav nav'
              'container_class' => 'navbar navbar-s-page'
            ]);
        } else {
            wp_nav_menu([
              'theme_location'  => $key,
              'menu_class'      => 'navbar-nav nav'
              'container_class' => 'navbar navbar-s-page'
            ]);
        }
    }

    If you like to use ternary operators:

    $menus = array(
        'motorcycle' => array(1, 4),
        'auto' => '3'
    );
    
    foreach( $menus as $key => $page_id ) {
    
        $theme_location = ( is_numeric($page_id) || is_array($page_id) && is_page($page_id) ) ? $key : 'primary';
        
        wp_nav_menu([
              'theme_location'  => $theme_location,
              'menu_class'      => 'navbar-nav nav'
              'container_class' => 'navbar navbar-s-page'
            ]);
    }

    I hope this helps.

    Thread Starter fromthe80s

    (@fromthe80s)

    I agree 100% with your initial disclaimer and appreciated the code examples. I needed to migrate a page quickly while practicing a bit of PHP and this will do. Many Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Different Menus for multiple Pages’ is closed to new replies.