Conditional secondary menu?

  • wilddoktor

    #1592

    I’m unsure if this is a theme or a WP question, but thought I’d ask here first.

    The site is http://www.thatcomputergeek.com/v1. I’ve created a main menu consisting of parent pages. I’m wondering if there’s a way to, when clicking on a menu item, bring up a secondary menu that only has pages based on that parent?

    For instance, when one clicks the “About” main menu item, the secondary menu would be the menu I’ve created with all the “About” sub-pages. But then when one clicks the “Admissions” main menu item, the secondary menu would switch to the menu I’ve created with all the “Admissions” sub-pages. And so on, for each menu item.

    Not convinced that’s the type of navigation I want, but I’d like to experiment with it if it’s possible.

    (As you can see from the site, I have already created menus for each parent that contain that parent’s sub-pages, and conditionally display those menus in sidebars using the awesome “Dynamic Widgets” plugin. That works; using a secondary menu would work better / look nicer. Again, just wanting to experiment a bit.)

    Thoughts?

    Thanks!

    Admin

    Syahir Hakim

    #16306

    Hurm..no straightforward way I suppose. I don’t think there’s any Dyanmic Widget-like plugin for custom menus yet.

    But you can do this in your child theme. Basically the idea is to add many more menus, each for the separate pages that you want. Then only show those menus when you’re on the page.

    This goes in child theme’s functions.php:

    <?php
    /* Register multiple secondary menus */
    register_nav_menus( array(
    'secondary-menu-page1' => __( 'Secondary Menu (Page 1)', 'graphene' ),
    'secondary-menu-page2' => __( 'Secondary Menu (Page 2)', 'graphene' ),
    'secondary-menu-page3' => __( 'Secondary Menu (Page 3)', 'graphene' ),
    ) );

    /* Conditionally display secondary menu */
    function graphene_multiple_secondary_menu( $args ){

    if ( is_page('page1') )
    $args = array_merge( $args, array( 'theme_location' => 'secondary-menu-page1' ) )
    if ( is_page('page2') )
    $args = array_merge( $args, array( 'theme_location' => 'secondary-menu-page2' ) )
    if ( is_page('page3') )
    $args = array_merge( $args, array( 'theme_location' => 'secondary-menu-page3' ) )

    return $args;
    }
    add_filter( 'graphene_secondary_menu_args', 'graphene_multiple_secondary_menu' );
    ?>

    This way you can register as manu custom secondary menus as you like, and have them appear only on the associated page.

    wilddoktor

    #16307

    You’re awesome…gonna try this now. 🙂

    wilddoktor

    #16308

    Ok; I think I see the logic, but am nonetheless a bit confused. I’m thinking that

    'secondary-menu-page1' => __( 'Secondary Menu (Page 1)', 'graphene' ),

    would translate in my case to

    'secondary-menu-about' => __( 'Secondary Menu (About)', 'graphene' ),

    and then

    if ( is_page('page1') )
    $args = array_merge( $args, array( 'theme_location' => 'secondary-menu-page1' ) )

    would translate in my case to

    if ( is_page('about') )
    $args = array_merge( $args, array( '/themes/tcgveritas' => 'secondary-menu-about' ) )

    But that’s obviously not correct, as I get a “Parse error: syntax error, unexpected T_IF” error in line 14 when I try it. 🙂

    Mind giving me another hint? I really appreciate your help!

    Admin

    Syahir Hakim

    #16309

    Oops…I forgot the semicolons..should be like this:

    if ( is_page('page1') )
    $args = array_merge( $args, array( 'theme_location' => 'secondary-menu-page1' ) );
    if ( is_page('page2') )
    $args = array_merge( $args, array( 'theme_location' => 'secondary-menu-page2' ) );
    if ( is_page('page3') )
    $args = array_merge( $args, array( 'theme_location' => 'secondary-menu-page3' ) );

    Also, don’t change the theme_location to your theme location! That’s actually a parameter for the wp_nav_menu() function call, so just leave it as theme_location. Just change the value it points to, like what you’ve already done above.

    wilddoktor

    #16310

    You, sir, are a genius! 🙂 It works now…thank you!

    Unfortunately, I just realized that once I click on a link in the secondary menu and go to that page…the secondary menu goes away. Oops…I hadn’t thought of that! So I’d need to create an array encompassing every page on my site, and then assign a secondary menu to each page.

    I don’t think I’ll do that. 🙂

    Ah, well; looks like I’ll just have to use the sidebar and dynamic widgets.

    Thank you so much for your help!!

Viewing 6 posts - 1 through 6 (of 6 total)

  • You must be logged in to reply to this topic.