Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1253417

    Kenrick C
    Participant

    URL: http://dev.smarternetworker.com/
    Latest version of X, Cornerstone & WP

    I want a specific menu named “Main Menu” to appear on my normal blog posts, archive pages and the home page.

    I added the following code (slightly modified) that I found in this thread: https://community.theme.co/forums/topic/custom-menus-on-different-pages/page/3/

    // =============================================================================
    // Sets Custom Menu For Non-Member Pages 
    // =============================================================================
    add_filter( 'wp_nav_menu_args', 'menu_switcher' );
    
    function menu_switcher( $args ) {
      if ( is_singular( 'post' ) || is_archive() || is_home() ) {
        $args['theme_location'] = 'primary';
        $args['menu'] = 'Main Menu';
      }
      return $args;
    }

    The name I used (Main Menu) is taken directly from the “Menu Name” field: http://prntscr.com/d5zoql

    Theoretically, my home page should now show a menu that has About, Products, Blog, Events, Contact, Login. However, it still shows my member’s menu which is just Login & Contact.

    Can anyone tell me why it may not be working?

    #1253788

    Jade
    Moderator

    Hi there,

    Would you mind providing us with login credentials so we can take a closer look? To do this, you can make a post with the following info:

    – Link to your site
    – WordPress Admin username / password

    Don’t forget to select Set as private reply. This ensures your information is only visible to our staff.

    #1256047

    Kenrick C
    Participant
    This reply has been marked as private.
    #1256099

    Rue Nel
    Moderator

    Hello There,

    Thanks for providing the information. Please take note that is_home will only points to the blog home page. You must update your code and use this instead:

    function menu_switcher( $args ) {
      if ( is_singular( 'post' ) || is_archive() || is_front_page() ) {
        $args['theme_location'] = 'primary';
        $args['menu'] = 'Main Menu';
      }
      return $args;
    }
    

    Please let us know if this works out for you.

    #1256102

    Kenrick C
    Participant

    Thanks Rue Nel! Works like a charm now.

    #1256154

    Christopher
    Moderator

    You’re welcome.

    #1256184

    Kenrick C
    Participant

    Hey guys, just ran into one problem with this. I wanted it to only affect the primary nav (top of screenshot) and not any other menus like the icon menu I use further down the page (an Ubermenu found on some pages) http://prntscr.com/d72kik.

    How would I exclude the filter from affecting anything besides the primary nav position?

    #1256323

    Paul R
    Moderator

    Hi Kenrick,

    You can specify what menu you like for your footer.

    Create file _nav-footer.php in wp-content/themes/x-child/framework/views/global
    Then copy the code below into that file.

    
    <?php
    
    // =============================================================================
    // VIEWS/GLOBAL/_NAV-FOOTER.PHP
    // -----------------------------------------------------------------------------
    // Outputs the footer nav.
    // =============================================================================
    
    ?>
    
    <?php
    
    if ( has_nav_menu( 'footer' ) ) :
      wp_nav_menu( array(
        'theme_location' => 'footer',
        'menu'           => 'Footer',
        'container'      => false,
        'menu_class'     => 'x-nav',
        'depth'          => 1
      ) );
    else :
      echo '<ul class="x-nav"><li><a href="' . home_url( '/' ) . 'wp-admin/nav-menus.php">Assign a Menu</a></li></ul>';
    endif;
    
    ?>
    

    Hope that helps

    #1256412

    Kenrick C
    Participant

    Hello Paul. I think you have misunderstood what I’m trying to do. I don’t need to put any menus in the footer.

    Please read the first post.

    The issue that I’m facing at the moment is that the code given by Themeco in post #1256099 works, but it tries to replace the primary navigation menu AND the menu in the middle of the page instead of just replacing the top menu (http://prntscr.com/d72kik).

    How would I make the code from post #1256099 ignore all of the other menus on a page besides the primary nav position (top menu)?

    #1256585

    Jade
    Moderator

    HI Kenrick,

    Please update the cdoe Rue suggested to:

    function menu_switcher( $args ) {
      if ( is_singular( 'post' ) || is_archive() || is_front_page() ) {
        if( 'primary' == $args['theme_location'] )
            $args['menu'] = 'Main Menu';
      }
    
      return $args;
    }

    Hope this helps.

    #1256596

    Kenrick C
    Participant

    Thanks Jade. That seems to have done what I need. How would I update the code if I need to add specific pages/post IDs?

    #1256598

    Kenrick C
    Participant

    And what about specific categories/tags?

    #1256652

    Thai
    Moderator

    Hi There,

    Please try with this code:

    function menu_switcher( $args ) {
      if ( is_single( array(123, 456) ) || is_page( array(567, 789) ) || is_tag( array('tag1', 'tag2') ) || is_category( array('cat1', 'cat2') ) || is_archive() || is_front_page() ) {
        if( 'primary' == $args['theme_location'] )
            $args['menu'] = 'Main Menu';
      }
      return $args;
    }

    Hope it helps 🙂