Conditionally Remove Your Primary & Secondary Nav Menu

One of the greatest features in WordPress is the navigation menu system. Its been upgraded recently which makes it even more flexible while taking a bit longer to master.

Different themes offer different locations where you can display your primary and secondary menu’s. On top of this, Genesis enables you to easily change the locations where your menu’s display. To extend the functionality of your site further, you may want to only display one of your nav menus on specific pages and remove them from others.

In this tutorial, you’ll learn how to use different conditional tags to remove either menu from any pages so it only displays on the ones you want.

Note: Please copy this code from the view raw link and paste it at the end of your child themes functions.php file using a text editor.

When Both Menu’s Display After Header

Assumes both your primary and secondary menu locations display after the header by default.

This code will remove the secondary (subnav) menu from your entire site excluding the page which matches the i.d in the code.

This code will remove the primary (nav) menu from your entire site excluding the blog page. Change the page name or page slug to exclude the menu from being removed on other pages.

Using conditional tags with different hooks in Genesis enables your to easily add any menu to any page and remove them from others.

When Primary Menu Displays Before Header

Assumes your primary menu displays before your header and the secondary menu displays after your header.

This code will remove the primary (nav) menu from your entire site excluding the blog page. Change the page name or page slug to exclude the menu from being removed for another page.

Secondary Category Menu Displays After Header

Assumes you’re using your secondary menu as your category menu and it displays after your header by default.

This code will remove the secondary menu (category menu) from your entire site excluding archives and single posts.

Note: When using the conditional tag for the blog page, you may find using the blog page name or slug doesn’t work. In that case, find the page i.d for your blog page and use that within the conditional tags brackets.
Example: !is_page('007')  ! Excludes the conditional

Menu Locations

All code assumes the primary navigation menu theme location is assigned the primary menu and the secondary navigation menu theme location is assigned the secondary menu.

menu theme locations

Genesis Simple Menu’s Plugin

Prefer using a plugin to code?

Here’s a post about how to use the Genesis Simple Menu plugin which enables you to manually display both of your primary and secondary menu’s on specific pages from your edit screens.

More Nav Menu Solutions


Comments

14 responses to “Conditionally Remove Your Primary & Secondary Nav Menu”

  1. […] Conditionally Remove Your Primary & Secondary Nav Menu […]

  2. […] Conditionally Remove Your Primary & Secondary Nav Menu […]

  3. Hey Brad,

    I am trying to remove my primary nav on my front page and a 3 other pages, but I am having issues removing it from even a single page. I believe I am using the correct hook, but maybe not. I have tried a few different, but same result. I basically want the WooCommerce page menu only on the shop pages, and removed from About Us, Contact etc. Here is a sample of what I tried, even for just one page.
    [code]
    add_action( ‘genesis_after_header’, ‘wpsites_after_header’ );
    function wpsites_after_header() {
    if (is_page(’26’)) {
    remove_action(‘genesis_after_header’, ‘genesis_do_nav’);
    }
    }
    [/code]
    To view the website:
    http://annibellelockets.flywheelsites.com/
    U: flywheel
    P: elastic-weight

    Thanks,
    Doug

    1. Sorry, I am using Pretty Chic theme.

      1. Hey Brad,

        Sorry to spam your comment section, but I figured it out. There was an entry in the functions.php re-positioning the nav that was overriding my entry. I updated to reflect the change I wanted and everything is working.

        Thanks,
        Doug

        1. Brad Dalton Avatar
          Brad Dalton

          No worries Doug

  4. Jami Mays Avatar
    Jami Mays

    I’ve added the Minimum Pro portfolio to a customized version of the Genesis Sample child theme (in dev, offline, an ecommerce site) and have also repositioned the secondary navigation to be situated before the Genesis loop (as it works beautifully as a product category menu).

    However, I’d like to hide the secondary navigation menu on the gallery page — but since it’s not a “page” and doesn’t have an ID, I’m not sure how I can hide the secondary navigation from the portfolio archive page and the single portfolio item posts.

    Can you help with this?

    1. Brad Dalton Avatar
      Brad Dalton

      Hello Jami

      Need access to your site to help.

  5. Above you said: “Using conditional tags with different hooks in Genesis enables your to easily add any menu to any page and remove them from others”

    I am trying to get a Genesis child theme to display a different menu as the primary nav menu on certain pages. Can you give me a hint as to how to add a different menu to certain pages?
    Thanks!

    1. Brad Dalton Avatar
      Brad Dalton

      Hi Ken

      You can use the Genesis Simple Menus plugin or PHP code to do this which would need to be written specifically for what you need to do.

      I have written and tested code for this using the is_user_logged_in() conditional which you could change to another one to suit your needs.

  6. Shawn Baran Avatar
    Shawn Baran

    Hi Brad,

    I’m trying remove the secondary nav from all the pages except for the blog category (including its children categories).

    The code to remove the primary nav works just as you said, however, I am unable to get any of the secondary nav codes to work. Do you know why that would be?

    1. Brad Dalton Avatar
      Brad Dalton

      Hi Shawn

      It could be because of a number of reasons.

      The hooks in the code only work on the old XHTML themes so if you’re running a HTML 5 supported child theme on Genesis 2.0, they won’t work. Simply change the hooks if this is the case.

      The conditional tag maybe wrong for what you need.

      Sometimes using ‘blog’ in the conditional doesn’t work so you may need to try the page slug or is_page_template(‘page_blog.php’)

      Another option is the blog page i.d.

      Or it could be that your secondary (subnav) isn’t set as your secondary in the menu settings.

      Hope that helps but generally it requires bit of playing around with and testing to get the right conditional working, especially for child category pages.

      1. Shawn Baran Avatar
        Shawn Baran

        Thanks for the reply Brad. I changed “genesis_after_header’ to ‘genesis_before_content_sidebar_wrap’ and it worked like a charm.

        Thanks again for this great code tip!

        Shawn

        1. Brad Dalton Avatar
          Brad Dalton

          No worries Shawn

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.