
Anyone who has ever used the Genesis framework to develop WordPress child themes understands the power, flexibility and security it provides to website development and maintenance. One true advantage is the ability to easily configure secondary menus or sub-menus on your site, if your child theme supports them. While secondary menus are all well and good, customers often require secondary menus to display only for specific posts or pages. This means developers have to approach sub-navigation menus conditionally. This requires some customization to the child theme. Let’s look at two approaches to solving this problem. They both produce the same results, but one creates a validation error, the other doesn’t.
Approach #1
The first approach is the easiest to implement and requires no customization at all. Functionally it works, but it will generate a W3C validation error. If W3C validation is as important to you as it is to me, you should probably jump to Approach #2.
This approach requires the Genesis Simple Menus plugin. This plugin allows you to select different secondary navigation menus for any post or page. Before we get started, make certain you have secondary navigation turn on in the Genesis Theme Settings:

Next, create and save an empty menu. I named the illustrated one "no-menu."

Finally, using the Genesis Simple Menus plugin, select the menu you just created for any page or post upon which you do not want to display the secondary navigation menu.

As I mentioned earlier, this approach works but it produces the following W3C validation error because system still creates the beginning and ending ul elements and there aren't any corresponding li elements for them to wrap around.

Approach #2
Approach #2 is a better option, but it requires inserting a few lines of custom code into your theme's functions.php file. In this example, the code references the page ID for the page upon which you want the secondary navigation menu to appear. The page id is an internal WordPress identifier which is hidden by default. One way to find the page id is to view the All Pages screen and hover your mouse over the name of the page. As you do this, look at your browser's status bar. The url of the page is displayed. The url includes text that reads "post.php?post=xxx" where the xxx is a numerical value representing the page id. This is not an easy way to find the page id, but this is WordPress and there are a lot of talented developers out there writing plugins that expand the platform's functionality. One such plugin is WP Show IDs by Jason of Primo Themes. Install this or any of the other "show id" plugins and your "All Pages" screen will look like this:

Now that you have the page id, copy the following code to the bottom of your theme's function.php file. Change the 8 to the page id of your target page and save the file.
You can use any criteria to drive the "conditions" for displaying the secondary menus. What I've shown you is a very simple example. If you choose to use categories or multiple pages, you'll have more coding to do, but at least now you have a head start for getting there. For example, for multiple pages, change the "if" statement to include an array of page numbers like if ( ! is_page( array( 8, 9, 20, 23 ) ) ). This shows the secondary menu only on the pages listed in the array.
If you would like to see the site where Approach #2 is implemented, please visit Bachman & Associates. Click on the Associates menu item to see the conditional secondary navigation menu. It appears for each of the associate's pages as well.
this doesn’t seem to work at limiting the pages shown on. The remove part works, but not the filter.
Is this information still viable with current Genesis ?
I’m assuming you’re talking about is_page() as the filter? Is_page() is a built-in WordPress function. It is not theme dependent. It works with any theme. You have to make certain the page IDs are correct. Here are the details in the WordPress codex: https://codex.wordpress.org/Function_Reference/is_page
I managed to get the code to using !is_page ( page numbers)
I am not able to get !is_page_templates to work.
This is what I am trying to do:
//* display secondary menu only on chosen page
add_action(‘template_redirect’, ‘child_conditional_actions’);
function child_conditional_actions() {
if ( !is_page(array( 337,339,325,327,329,344,341,350,325,333)&&(!is_page_template(array(‘single-projects.php,archive-projects.php’)))
remove_action(‘genesis_after_header’, ‘genesis_do_subnav’);
}
I get a white screen. It must be some simple thing.
A white screen indicates a PHP coding error. In this case, it is with is_page_template. Is_page_template does not accept an array as an argument.
Apparently you can only add so many page numbers before you must label it as an array?
I used your code below, and added pages, then it stopped working, so I added the (array(.
That works. But it still leaves me without the menu on the single posts templates.
When I try if (!is_page_template(single-projects.php)) -instead of the code using page numbers- nothing happens. Putting the single-projects in single quotes, or not, doesn’t seem to affect it.
I need to have the menu appear on certain pages as well as on single custom posts and archives.
I have tried various combinations for hours. I thought this would be a rather simple thing to do.
It all seems very unstable. Sometimes it white screens, sometimes it doesn’t, and it is not at all obvious why. I am wring the code in a IDE and using FTP.
I thank you for your help- I didn’t find this info anywhere else, I would have thought Studio Press would have it available in their site.
You are using a logical AND (&&) in your if statement. This means that both sides of the statement have to be true at the same time. As I read the if statement, I understand it to say, don’t display the secondary menu if the page ID is not in this array AND the template for the current page is not single-projects. Is that really what you’re trying to accomplish?
I need the menu to display on 10 pages, as well as all custom posts pages and archives. I do not want it to display anywhere else on the site. It’s most important for it to appear on the posts pages. But that is the one thing I cannot accomplish.
Looking at it in the IDE- it appears that PHP really doesn’t work well with an if statement followed by the remove.
As I added and removed the function from the functions file, some times it was okay, and sometims it wasn’t, even though the code was exactly the same.
If you have more than a few page ID’s, it has to be set up as an array. Doing is_page_templates didn’t do anything.
It might be easier for you to use the Genesis Simple Menus plugin. You can add the menu to the page in the editor.
hi there and thanks for this info! I’ve been reading posts for a while now but i’m a total beginner so i’m still learning to modify files.
I’ve found the id of my pages- how would i remove the secondary menu on several pages?
thanks
Add the following code to your theme’s functions.php file.
/** display secondary menu only on chosen page **/
add_action('template_redirect', 'child_conditional_actions');
function child_conditional_actions() {
if ( !is_page( 8,9,20,23) )
remove_action('genesis_after_header', 'genesis_do_subnav');
}
Change the numbers in the list to the ID numbers of the pages you want the menu to display on.
Thank you for this tutorial! I’m having trouble getting it to display on multiple pages though. I’ve tried changing the “if” statement to: if ( !is_page( 8,9,20,23) ) and also tried: ( !is_page( array(8,9,20,23) ) ) but neither work – each seems to show the nav bar on ONE of the pages listed, seemingly randomly, but not on ALL of the pages listed. How can I get them to show on multiple pages??
You have a logical not (!) in front of is_page(), but your comment seems to indicate you that you want the nav bar to display on the listed pages. The not means “don’t display” on these pages. Remove the not to display on the pages.
Hi Victor – I would like to select which menu to display as the secondary menu depending on the page. I can do the conditional part depending on the page but not sure how to refer to *which* of my custom menus to use as the subnav (e.g. I have defined menu1, menu2, menu3, etc – how do I tell genesis use this menu?)
Specifically, the plugin addresses only secondary navigation, not primary.
Good start with this post. But can you offer any advice on REPLACING the menu you removed via code for the PRIMARY navigation?
To replace the primary navigation with a different menu depending upon the page ID or the page template, that would complete the task.
Thanks in advance for suggesting some code to accomplish this.
Great article, thanks! I was wondering if there were other conditional arguments you can use when choosing to display a secondary menu. My school’s website is going to be desgined around three “users” – parents, teachers and students. Is there a way of checking to see which parent a page has and display a secondary menu conditionally for a whole range of pages?
Thanks!
What you are asking to do is pretty complex, but without seeing the written specs, I’m not sure that what you want to do at an individual parent level of granularity is feasible with WordPress and the Genesis framework without some sort of membership management plugin. Many schools today put up a simple front end site and leave the more complex things to Renweb. My daughter’s school uses Renweb and as a parent, I access it regularly. It’s a pretty good system.