Adding Multiple Menus in a Template Built From Scratch
-
Hello,
I am creating a WordPress Template from scratch.
I have a main menu running along the bottom of my header and it works perfectly. But I need to add 2 additional menus each only for a specific page. All of them need to be editable by going into the backend then navigating to: appearances –> menusI have added this code to my functions.php:
register_nav_menus( array(
‘primary’ => __( ‘Primary Navigation’),
‘services-nav’ => __( ‘Services Navigation’),
‘ourteam-nav’ => __( ‘Our Team Navigation’),
) );I have then embedded this code into the custom page template services-page.php:
<?php wp_nav_menu( array( ‘theme_location’ => ‘Services Navigation’ ) ); ?>
- The Issue
The Primary Navigation/Main Navigation that runs along the bottom of the header only shows the pages I have selected in the appearances –> menus
but
the Services Navigation displays all of the pages I have selected for the Primary Navigation plus the three I have selected for the Services Navigation.
Does someone have a fix? Keep in mind I am a noob when it comes to the php coding language.
To See A Live Example of the Issue: CLICK HERE
I am not sure how long this link will work as it is my development URL
-
In your template file where you have your menu code add a conditional is_page() wrap around the menu code. Here is an example, ( this is meant only to guide, not a verbatim, copy and paste, solution. div and nav are included as a reference only to help you understand):
if( is_page( ‘dispatch-consolidation’ ) ) {
<nav class=”dispatch-menu”>
[YOUR Services Navigation MENU CODE]
</nav>
}if( is_page( ‘team’ ) ) {
<div class=’team-menu’>
[YOUR team Navigation MENU CODE]
</div>
}Remove this (and for your team menu also):
<?php wp_nav_menu( array( ‘theme_location’ => ‘Services Navigation’ ) ); ?>as it points to where on the page the menu should be displayed, not which page it should be displayed on.
I am doing something wrong I tried that a bunch of times and ways I could think of and kept getting syntax errors
This is my code for my Team Page Template:
<?php
/*
Template Name: Our Team Page
*/get_header(); ?>
<div id=”TeamPageBackground”>
<div id=”TeamPageNav”>
<div>
<h2>
OUR TEAM
</h2>
</div><?php wp_nav_menu( array( ‘theme_location’ => ‘Our Team Navigation’ ) ); ?>
</div>
<div id=”TeamPageContent”>
<?php get_template_part( ‘loop’, ‘page’ ); ?>
</div>
</div>
<?php get_footer(); ?>
Do:
if( is_page( ‘team’ ) ) {
<div id=”TeamPageNav”>
[YOUR team Navigation MENU CODE]
</div>
}where ‘team’ is the slug of your page. If you are not using a slug in the admin section, add it and try again.
Remove:
<? php wp_nav_menu( array( ‘theme_location’ => ‘Our Team Navigation’ ) ); ?>And report what error you are getting. Sorry, this is rushed as I have a meeting now. I apologize, I will not be available for a few hours…
I just had an Ah Ha Moment!
O.k. here is my mistake.
I had put: ‘theme_location’ => ‘Services Navigation’
Services Navigation is the human readable name of the menu not what I registered it as in my functions.php as follows:
register_nav_menus( array(
‘primary’ => __( ‘Primary Navigation’),
‘services-nav’ => __( ‘Services Navigation’),
‘ourteam-nav’ => __( ‘Our Team Navigation’),
) );So the code in services-page.php template should have been:
‘theme_location’ => ‘services-nav’
Thanks DMBarber for helping me out I appreciate it a lot. If I didn’t have someone to talk to about this I would have never figured it out.
- The topic ‘Adding Multiple Menus in a Template Built From Scratch’ is closed to new replies.