Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Add Conditional Logic to Menus in WordPress

Do you want to show different navigation menu items for different pages in WordPress? Perhaps showing a different menu to logged in users, or hiding a menu item on particular pages? Normally it would require you to add code snippets or use CSS to hide menu items, but wouldn’t it be great if you could do that inside your WordPress admin area? In this article, we will show you how to add conditional logic to menus in WordPress.

Video Tutorial

Subscribe to WPBeginner

If you don’t like the video or need more instructions, then continue reading.
First thing you need to do is install and activate the If Menu plugin. It works out of the box. All you need to do is visit Appearance » Menus and start editing your navigation menus.

Start by selecting an item in the navigation menu and click the ‘Enable Conditional Logic’ checkbox. This will show the conditional menu, which looks like this:

Conditional menu

Next, choose whether you want to show or hide that item and then choose the condition. For example, only show the logout page link when a user is logged in.

After choosing a condition, simply press the save menu button to store your changes.

Adding Your Own Conditions

If Menu is a new plugin, and there are not many conditions that you can apply. WordPress provides many more conditional tags that you can use in your projects. For a full list check out this page.

You can add any of these conditional tags to be used with the If Menu plugin. For example, let’s assume that you want to show/hide a menu item on custom post type pages.

You would add the code snippet to your theme’s functions.php file or a site-specific plugin like this.

add_filter( 'if_menu_conditions', 'wpb_new_menu_conditions' );

function wpb_new_menu_conditions( $conditions ) {
  $conditions[] = array(
    'name'    =>  'If it is Custom Post Type archive', // name of the condition
    'condition' =>  function($item) {          // callback - must return TRUE or FALSE
      return is_post_type_archive();
    }
  );

  return $conditions;
}

This is how it would appear in the conditional logic menu.

Adding your own conditions in the conditional menus

That’s all. We hope this article helped you add conditional logic to menus in WordPress. You may also want to check out our guide on how to style WordPress navigation menus.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

33 CommentsLeave a Reply

  1. I don’t see the “Enable conditional logic” under Appearance -> Menu. I’m using WordPress 5.7 as of March 16, 2021.

    • The wording may have changed since this article but there should still be a checkbox for changing the visibility.

      Admin

  2. BIG Thank you for this article, it’s awesome.
    Your snippet of code was perfect for a project I was working on, as was this plugin which I’m now using.
    Even in 2019 this is a handy page, it’s working well on WordPress 5.2.3 (Astra Pro theme) so still very applicable.

  3. I would like to ask I have added role for user Author and subscriber both then its should be display to author not subscriber. how to handle this?

  4. Really simple and easy to use plugin. Fantastic! It did not only save me a lot of time but also an organizational advantage because instead of using widget logic with different menu widgets, I create now one 1 menu with conditions per item.

  5. Thank you,

    I have only used this plugin for conditions for users that are logged in or not logged in, but it works great, it really has saved me hours of messing around with code.

  6. Could I set up this plugin so that I can set up different menu options for each different category?

  7. Thank you guys for sharing this. However it will display a PHP Notice on conditioned menu items and the custom condition won’t be displayed among condition options, if you don’t specify the id parameter.

  8. Hi, I added conditions (if a string matches the server name, a menu is hidden). Great! But I would like to add even more custom conditions. Could you show an example of the code you would use to add more than on name/condition pair the custom option?

    name = “If the site is not www.”
    condition = www is not in SERVER_NAME
    name = “Paul site.”
    condition = “paul is in SERVER_NAME”

    etc.

    Thanks!

  9. This Conflicts the WP User Manager Pro, and then user manager Pro plugin functions not working due to the confliction.
    as an ex: Show password tick in user registration page doesn’t work, as well as login page also not functioning well.
    can you please fix this

  10. Yep, definitely broken.

    Works great for the built in conditions, but when coding custom conditions it works once and then stops working.

  11. YES!!! After 90 minutes of trying to figure this out, this article made it work for me. THANK YOU!!!

  12. Hello
    I installed this plugin, but I am getting an warning as follows :

    Warning: Missing argument 2 for custom_nav_edit_walker() in /var/www/equest/wp-content/themes/wp-questrian/framework/megamenu/mega-menu.php on line 42

    How to solve this warning ? Please guide me.

  13. I have not used the If Menu plugn, but I tried the Nav Menu Roles plugin

    It works but then conflicts with the Mega menu functionality of my theme.

    I am hesitant to try the If Menu plugin mainly because it has not been updated in so long. It will be a matter of time before I have to remove it for sure.

  14. Could not get this plugin to work. Not showing up i menu-items. Using: WP 4.3 / Nimwa theme.

    Any suggestions on similar working plugin? Just want to hide some menu items while working on them…

  15. Hi,
    Could this be achieved with this plugin,
    I want to have different menu for different pages or post. for example,

    Home | About Us | Service | Contact

    About Us [ History | Team | Career ]
    Services [ Web Development | Window Application | Corporate ID ]

    Is it possible with this plugin to show sub menu on side menu for About Us

      • I stopped installation after reading WP Org PlugIn Page, there is the comment “1 person says it is broken” and in the support section is an open report which is not solved.
        If I read something like this I don’t even try to install such plug in.

      • The plugin is not working. It’s not showing “enable conditional logic” and the plugin author is not responding from last 7 months.

        Tried with disabling other plugins. But no result.

        Is there any other plugin to do so!?
        I need it.

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.