Some time ago I wrote a “how to" article titled Use Conditional Secondary Menus in Genesis Themes. That article has proven to be very popular receiving thousands of page views. It has also generated many questions from people leaving comments and sending emails. This post revisits the topic of Using Conditional Menus in Genesis and answers the three most frequently asked questions compiled from those you've requested.
Genesis Conditional Menu Q1
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?)
How To Display Secondary Conditional Menu for Specific Page(s)
To test for a specific page ID, use the conditional is_page() function. The is_page() function can test for a valid page by page ID, title, slug or variable conditions contained in an array. The following code displays a different secondary conditional menu based on the page ID of a page selected from the primary menu. The code was tested on the Genesis Parallax Pro Theme and the Genesis Executive Pro Theme.
How to Use is_page()
- Using is_page(); without any parameters returns true when any single Page is being displayed.
- Inserting a page ID as a parameter (i.e. is_page( 42 );), returns true when that specific page is being displayed. To determine the page ID, install a plugin like Reveal IDs by Oliver Schloebe to display the ID on the All Pages panel.
- You can test for a specific page's title, known to WordPress as the post_title, by using is_page( 'Contact' );
- You can test for a specific page's name (slug) by using is_page( 'about-me' );
- To cover all of your bases, test for a specific page by creating an array that includes post ID (42), or post_name ("about-me") or post_title ("Contact") by using is_page( array( 42, 'about-me', 'Contact' ) );
You need to be very careful that you don't inadvertently pass an empty parameter to is_page(). Empty parameters always produce a true result. For example, all of the following entries will produce a true result:
- is_page( '' )
- is_page( 0 )
- is_page( '0' )
- is_page( null )
- is_page( false )
- is_page( array() )
Genesis Conditional Menu Q2
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?
How To Display a Primary Conditional Menu for Specific Page(s)
As with secondary conditional menus above, to test for a specific page ID to display a primary conditional menu, use the conditional is_page() function. The is_page() function can test for a valid page by page ID, title, slug or variable conditions contained in an array.
How to Use is_page_template()
An alternative to is_page() is the is_page_template() function. To test for a specific page template, use is_page_template( 'about.php' ). The parameter is the name of the template file. If a template is located in a subdirectory, pass the path as the parameter as in is_page_template('templates/about.php').
The same code used above for secondary conditional menus may be modified to change the primary conditional menu as in the following example:
Genesis Conditional Menu Q3
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 designed 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?
How To Display a Secondary Conditional Menu for Specific SubPage(s)
If I understand your question correctly, you want to test a subpage for its parent page and then display a secondary menu based on that parent page. The first thing to know is that WordPress does not have a native function to test for a subpage, so you'll have to write a function to include in your theme's functions.php file. This code block hasn't been tested on any of my sites, but the is_subpage() function is based on an example from the WordPress Codex, so it should work.
These conditional menu debugging tips come from my experience as I was testing this code. I've included them to make it easier for you to solve similar problems you may experience as you implement conditional menus on your site.
Initially Populate Menu Location
For these solutions to work properly, you should make sure you have an initial menu set in the location you want to change conditionally. Set the initial menu on the Appearance/Menus admin page. If you don't have an initial menu, the line of code that tests for the menu location, if ( $args['theme_location'] == 'secondary' ), will return false and the code won't execute.
Secondary Menu Disappears After Selecting Item
Make sure you include the page ids for the pages assigned to the conditional secondary menu. The code above is only testing for the page id when you click a primary menu item. You will see the secondary menu appear where it is supposed to, but as as soon as you click any secondary menu item, the secondary menu will disappear. The page you want will display, but the menu will be gone. Why? Because you haven't accounted for the secondary menu page ids. The solution is to expand your is_Page() test to include the page_ids for the secondary menu (i.e. is_page( 37, 3545, 251, 309 ) ).
This site uses the Genesis Executive Pro Theme. The Executive Pro Theme allows users to set up a portfolio page like this one //victorfont.com/portfolio/. This page does not exist until run time. It is generated by Genesis when a visitor selects it from a menu. Its related content are custom post types that you create as you would any other post or page from the Portfolio/Add New link on the Admin menu. To test for a portfolio page, you would have to use is_page_template( 'archive-portfolio.php' ).
To take this further, let's say you create a secondary menu for a portfolio page that displays a link to each of items on the portfolio page. Test for all of these with if ( is_page_template( 'archive-portfolio.php' ) || is_page( 1,2,3,4,5) ). The double pipe ( || ) is a logical "OR." This code reads, "Display the secondary menu 'IF' a user clicks on the portfolio page 'OR' any of the pages I have listed on the secondary menu."
In most case, the Home menu item is a link to your site's URL. WordPress has two conditional tags that could work to test for whether you are on the home page or not. They are is_front_page() and is_home(). In fact, WordPress has many built-in conditional tags that you could use to test for various conditions in your conditional menu code. To learn more, I suggest you visit Conditional Tags.