WordPress.org

Support

Support » Themes and Templates » Custom Menus on Different Pages

Custom Menus on Different Pages

  • I am developing a website for a client using WP and would like to display different menus on different pages. So for instance if a user selected the Profile page the sidebar would display the profile menu, if they selected the services page it would display the services menu and so on…

    I can create a custom menu and have also looked at custom page templates but I can’t work out how to add a specific custom menu to individual pages, or groups of pages.

    Firstly I was wondering if this was indead possible and if it is, some idea of how to do it would be really appreciated.

Viewing 15 replies - 1 through 15 (of 22 total)
  • First make sure you have registered your menus in functions.php:

    //register the custom menus
    function register_my_menus() {
    register_nav_menus(
    array(
    'profile-menu' => __( 'Profile Menu' )
    'services-menu' => __( 'Services Menu' )
    )
    );
    }
    add_action( 'init', 'register_my_menus' );

    Customise each menu in the wordpress backend.
    Then call the relevant menu using an if statement in the sidebar (or wherever you want the menus to appear).

    <?php
    if (is_page('profile')){
    wp_nav_menu(array('menu'=>'profile-menu' ));
    } elseif (is_page('services')) {
    wp_nav_menu(array('menu'=>'services-menu' ));
    }
    ?>

    ensure the string in the is_page() function matches the slug (or id) of the page you want to check.

    Hi Miocene, thanks for the quick reply, I will give that a go and let you know how I get on… cheers

    I’ve been needing the same thing, a different menu for particular pages. I wanted to avoid having to have named pages in conditional statements though so have gone for something different, that means I don’t have to change the theme to add a new page/menu pair.

    In the theme I have:

    <?php wp_nav_menu( array( ‘container’ => ‘none’, ‘container_class’ => ‘menu-header’, ‘theme_location’ => ‘primary’, ‘menu’ => get_post_meta( $post->ID, ‘MenuName’, true) ) ); ?>

    Then in the page I simply have a custom field called “MenuName”, which is set to match the name of the wordpress nav_menu I want used on that page.

    If a custom MenuName is not defined then it will fall back to the nav_menu assigned to that theme_location.

    teknohippy I like your approach but you still have to set up the name of the different custom menus on the functions.php page don’t you? Which is fine if you’re a techy person, but if you’re handing the wp site over to a non-techy client then it’s not ideal either.

    I’ve been trying to work out the ideal front-end user solution to this. I wonder whether widgetising the theme in the place you want the menu to go, then make the menus in the admin, then add these menus to the widgetised area using the menu widget. Then you’d need to get the widget logic plugin to do the conditional statements in a “friendly” non-techy way.

    What does anyone think??

    ok, just tested it and you don’t need to add the names into functions.php, so it’s a good solution. Thanks!

    One question – if i don’t define a custom MenuName in a page or post then I don’t want it to show any menu. Can you tell me how to do this?

    If I don’t add a custom field for a menu then it seems to just default back to the one assigned to that location in the Appearance -> Menus.

    I was quite pleased to see that happen without me having to code anything. *grins*

    teknohippy – you’re right that if you don’t add a custom field for a menu then it defaults back to the one assigned to that location in the Appearance -> Menus. But this is what i *don’t* want to happen. I want it to default to no menu at all, including no html code.

    I found the answer, in case anyone else want sto know… just use the fallback_cb code but with no value:

    'fallback_cb' => ''

    So my sidebar.php file looks like this now:

    <!-- custom fields need to be inside the loop -->
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <?php wp_nav_menu( array( 'fallback_cb' => '', 'theme_location' => 'sub-navigation', 'menu' => get_post_meta( $post->ID, 'MenuName', true) ) ); ?>
    
    content here as well
    <?php endwhile; ?>

    Thanks for all the answers, I think you guys are FAR more technical than me… I’m starting to understand bits of this but I have to be honest I’m not getting it all. Im going to try what you have suggested again and will post again with any issues, however stupid they may seem to me… I hope that’s OK!

    Mark

    @markurbaninteractiveus

    Hmmm…
    What does “In the theme I have:” mean? What file does this line of code go into? I’ve tracked down the primary menu code in the header.php, but not the secondary menu code that teknohippy refers too. Using twenty eleven. Maybe that’s the issue?

    Mark…

    This code is in the header file:

    <?php wp_nav_menu( array( 'container' => 'none', 'container_class' => 'menu-header', 'theme_location' => 'primary', 'menu' => get_post_meta( $post->ID, 'MenuName', true) ) ); ?>

    Then in the admin site you can add new menus under the appearance section. These menus you add will have names.

    Then if you require a different menu on a given page you need to make sure first that you can see the Custom Fields section of the page. Check the Screen Options link at the top to make sure you can see it.

    Then create a new custom field called MenuName and give it a value matching the name of the menu you want that page to use.

    Mark

    @markurbaninteractiveus

    Thanks teknohipppy. I see now that this topic concerned the primary menu while I thought it was about the secondary menu in the sidebar. I think I found a plugin “Custom sidebars” to take care of my issue.

    It’d work for any theme_location.

    Glad you’ve found a solution.

    Cheers
    Iain

    teknohippy, your solution indeed looks like poetry! As one who is at the beginning here, I need a couple more pieces:

    > …Then in the page I simply have a custom field called “MenuName”, which is set to match the name of the wordpress nav_menu I want used on that page.

    IF I created a MenuName: “123”, what is the code / syntax for creating a custom field for “MenuName” and setting MenuName to (“123”)?

    Custom fields are added in a posts edit page.

    If you can’t see them check “screen options” at the top of the edit page.

    Example image

    You’ll see any existing custom fields there as above, although you will most likely not have any.

    Just click “Add Custom Field” button to create a new one.

    Use “MenuName” for the Key and the value should be the name of the Menu you want to use, the name you gave the menu in the “Appearances -> Menus” section

    Thank you, Teknohippy! I appreciate the detail for the custom fields.

    RE: This code is in the header file:
    —————–
    <?php wp_nav_menu( array( ‘container’ => ‘none’, ‘container_class’ => ‘menu-header’, ‘theme_location’ => ‘primary’, ‘menu’ => get_post_meta( $post->ID, ‘MenuName’, true) ) ); ?>
    —————–
    WHERE???

    I tried placing this code in ‘header.php’ and ‘functions.php’ in the Twenty Ten theme. Both locations crashed the site (but I did hear a deafening round of applause for having backed up the entire site and restoring the incorrectly edited files).

    Where should it be placed? And, does any of this code contain an example name, which needs to be edited for a particular site?

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Custom Menus on Different Pages’ is closed to new replies.