• Skip to main content
  • Skip to primary sidebar

Victor Font Consulting Group, LLC

The DEX Intranet Specialists

  • Home
  • Care Plans
    • Care Articles
    • Optional Subscriptions
  • Consultations
  • Products
    • Code Snippets
    • Public GitHub Repositories
    • Gist Snippets
    • Pastebin Snippets (Free)
    • Free Plugins
  • FAQs
  • Support
    • Graphic Design
  • Contact
    • Speakers
    • Portfolio
  • Resources
    • Free WordPress Video Training
    • Tutorials
    • Articles
    • Cybersecurity
    • EU Referral Network

Genesis Conditional Menus Revisited

February 5, 2014 By Victor M. Font Jr.

Contents
  • Overview
  • Genesis Conditional Menu Q1
    • How To Display Secondary Conditional Menu for Specific Page(s)
      • How to Use is_page()
  • Genesis Conditional Menu Q2
    • How To Display a Primary Conditional Menu for Specific Page(s)
    • How to Use is_page_template()
  • Genesis Conditional Menu Q3
    • How To Display a Secondary Conditional Menu for Specific SubPage(s)
  • Debugging Tips
    • Initially Populate Menu Location
    • Secondary Menu Disappears After Selecting Item
    • Portfolio Pages
    • Home Page

Overview

Genesis Conditional Menus Revisited

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.

Debugging Tips

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 ) ).

Portfolio Pages

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."

Home Page

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.

Use Conditional Secondary Menus in Genesis Themes
  • 6shares
  • Facebook0
  • Twitter0
  • Pinterest0
  • LinkedIn6
  • Print
  • SMS0

Filed Under: Child Themes, Code Snippet, Computers and Internet, Conditional Menus, Genesis, Genesis-Slider, How To, Programming, WordPress Tagged With: Child Themes, Code Snippet, Computers and Internet, Genesis, How To, Programming, WordPress

About Victor M. Font Jr.

Victor M. Font Jr. is an award winning author, entrepreneur, and Senior IT Executive. A Founding Board Member of the North Carolina Executive Roundtable, he has served on the Board of Advisors, of the North Carolina Technology Association, the International Institute of Business Analysis, Association of Information Technology Professionals, Toastmasters International, and the North Carolina Commission for Mental Health, Developmental Disabilities, and Substance Abuse Services. He is author of several books including The Ultimate Guide to the SDLC and Winning With WordPress Basics, and Cybersecurity.

Primary Sidebar

Shopping Cart

Books

  • Ultimate Guide to the SDLC front cover The Ultimate Guide to the SDLC
    Rated 5.00 out of 5
    $74.95
  • Winning With WordPress Basics 2nd Edition Winning With WordPress Basics 2nd Edition $19.95

Recent Articles

  • Modern Scam Defense: How Consumers and Businesses Can Recognize and Stop Email, Phone, and Text Fraud
  • How to Write a PRD So Dense It’s Technically a Novel
  • Top 5 Plugin Names That Scare Our Legal Department
  • When Agile Meets Our 3-Year Waterfall Roadmap: A Love Story
  • Why Our Enterprise Needs 27 Stakeholders to Approve a Button Color Change

Top 10 Article Categories

Best Practice Code Snippet Computers and Internet Genesis How To Leadership Programming Servant Leadership Tutorial WordPress

 
We only use analytical cookies on our website that allow us to recognize and count the number of visitors, but they do not identify you individually. They help us to improve the way our website works. By clicking Accept you, agree to cookies being used in accordance with our Cookie Policy.