0

I'm looking to create and display a separate menu for a specific page. This menu needs to be in the primary location. So for example, on the Home page and the Contact page you would have: Home | About us | Contact. And on the "About us" page, you could show a different menu for example: Menu link 1 | Menu link 2 | Menu link 3. I have been trying for ours to do this with no luck. I literally have been looking at this for so long I don't even know where to start anymore. :( I'm hoping it's something like the code below.. Anyway, any help would be greatly appreciated. Thanks

function my_example_menu() {
  register_nav_menu('example-menu',__( 'Example Menu' ));
}
add_action( 'init', 'my_example_menu' );

wp_nav_menu( array( 
    'theme_location' => 'my-example-menu', 
    'container_class' => 'my-example-menu-class' ) ); 

function my_display_example_menu() {
  if (is_page('about-us')) {
  unregister_nav_menu( 'Primary Navigation' );
  register_nav_menu( 'Example Menu' );
  }
}

//EDIT

Parent theme nav

<?php truethemes_before_primary_navigation_hook();// action hook ?>

<nav role="navigation">
<?php if('true' == $ubermenu):
    wp_nav_menu(array(
        'theme_location' => 'Primary Navigation' ,
        'depth'          => 0 ,
        'container'      => false ,
        'walker'         => new description_walker() ));
else: ?>
<ul id="menu-main-nav" class="sf-menu">
<?php wp_nav_menu(array(
        'theme_location' => 'Primary Navigation' ,
        'depth'          => 0 ,
        'container'      => false ,
        'walker'         => new description_walker() )); ?>
</ul>
<?php endif; //end uberMenu check  ?>
</nav>
<?php truethemes_after_primary_navigation_hook();// action hook ?>
  • Is your wp_nav_menu() in your functions.php? It should be in a template file, usually header.php. Also, is my_display_example_menu() hooked to an action? If it's not, then how are you calling that function? – Dan. Nov 29 '16 at 14:16
  • Also, is_page('page_slug') won't work in functions.php. For you situation, I suggest not using the method of unregistering a menu on specific pages. I think you should create a custom field, for pages/posts, where you will select which menu to use on that page/post. And then, write a bit of code in the template file which will read the value of the custom field and pass the corresponding parameter to wp_nav_menu(). – Dan. Nov 29 '16 at 14:17
  • Hi Dan, yes it is page slug and yes it's in functions.php because it's a child theme. there is a hook in header.php for before or after navigation. I have tried both but had no luck with it. It's me doing something wrong I'm sure. My WP PHP is pretty basic. – user53340 Nov 29 '16 at 14:19
  • In the parent theme's header.php what is in between the two hook? Could you paste it into your question, including the two hooks – Dan. Nov 29 '16 at 14:22
  • Ok. I think still go down the 'custom field' method. I'll write an answer now. – Dan. Nov 29 '16 at 14:23
1

As you can read from the documentation register_nav_menu is used to register a menu in the menu editor. Your code will affect the backend only. Also you should pass a slug like string as the first parameter. If you want to change the display of the menu, you could simple write a condition which will change the parameters of wp_nav_menu. Like the following:

// header.php (or similar)
wp_nav_menu([
  'menu' => (is_page('about-us') ? 'primary-navigation' ? 'example-menu'),
]);

Otherwise you could also register a menu and change the value of the theme_location parameter to make the menu configurable by the user.

Update:

To achieve this via a filter hook

// functions.php
add_filter('wp_nav_menu_args', function ($args) {
  if (is_page('about-us')) {
    $args['menu'] = 'my-custom-menu';
  }
  return $args;
});
  • Thanks for the answer, will this work in functions.php? – user53340 Nov 29 '16 at 14:27
  • No, because wp_nav_menu is called in the template files. Not in the functions.php. The function is used for displaying a menu. If you want to change the menu from your functions.php you can hook into wp_nav_menu_args – Fabian Marz Nov 29 '16 at 14:31
  • This answer is a quick fix, but it is hard-coding/not what a CMS is about. If you end up with 20 menus that you want to be able to assign to different pages, this method will become a pain. – Dan. Nov 29 '16 at 14:33
  • No, that's the correct entry point if you want to adjust the menu from your functions code. Instead of repeating multiple lines (like you suggested) you could easily implement something like the following: – Fabian Marz Nov 29 '16 at 14:37
  • ` add_filter('wp_nav_menu_args', function ($args) { $post = get_post(); if ($custom_menu = $post->_custom_menu_id) { $args['menu'] = $custom_menu; } return $args; });` – Fabian Marz Nov 29 '16 at 14:42
0

1 - Copy the parent theme's header.php into your child theme.

2 - To create custom fields, if you are not too knowledgable of WP, I suggest using the Advanced Custom Fields plugin.

Read the docs on their website. You want to create a select/dropdown or radio type field and have it for all post types and pages.

For each menu that you have, give the custom field a value.

Give the custom field a default value - the one for the menu that you will be using most (so that it'll save you some time)

3 - Give the other pages/posts the other menu.

4 - In your child theme's header.php, change this:

<?php if('true' == $ubermenu):
    wp_nav_menu(array(
    'theme_location' => 'Primary Navigation' ,
    'depth'          => 0 ,
    'container'      => false ,
    'walker'         => new description_walker() ));
else: ?>
 <ul id="menu-main-nav" class="sf-menu">
 <?php wp_nav_menu(array(
    'theme_location' => 'Primary Navigation' ,
    'depth'          => 0 ,
    'container'      => false ,
    'walker'         => new description_walker() )); ?>
</ul>
<?php endif; //end uberMenu check  ?>

To:

<?php 
$menu_to_use = get_field(get_the_ID(), 'meta_key_of_custom_field');
if('true' == $ubermenu):
    wp_nav_menu(array(
    'theme_location' => $menu_to_use, // make sure that the values of the custom field are the names of the menus (e.g. Primary Menu)
    'depth'          => 0 ,
    'container'      => false ,
    'walker'         => new description_walker() ));
else: ?>
 <ul id="menu-main-nav" class="sf-menu">
 <?php wp_nav_menu(array(
    'theme_location' => $menu_to_use, // make sure that the values of the custom field are the names of the menus (e.g. Primary Menu)
    'depth'          => 0 ,
    'container'      => false ,
    'walker'         => new description_walker() )); ?>
</ul>
<?php endif; //end uberMenu check  ?>
  • Thanks Dan, I'll look into this and promise I'll come back and mark your answer correct if it works. I've just been pulled off to do something else by the boss :( Thanks again for the answers and explanations. – user53340 Nov 29 '16 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.