WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Plugins» How to Add Conditional Logic to Menus in WordPress

How to Add Conditional Logic to Menus in WordPress

Last updated on May 26th, 2015 by Editorial Staff
203 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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+.

203 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

31 Comments

Leave a Reply
  1. Laurentiu says:
    Oct 26, 2020 at 1:08 pm

    A small feature, but very missed when a programmer is asked to work in wordpress.

    Reply
    • WPBeginner Support says:
      Oct 27, 2020 at 9:33 am

      Glad you found this recommendation helpful :)

      Reply
  2. Richard Spatts says:
    Mar 18, 2020 at 6:47 pm

    Thanks for this, it really helped

    Reply
    • WPBeginner Support says:
      Mar 19, 2020 at 8:51 am

      You’re welcome, glad our guide helped :)

      Reply
  3. Richard S. says:
    Oct 5, 2019 at 7:39 am

    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.

    Reply
    • WPBeginner Support says:
      Oct 7, 2019 at 10:38 am

      You’re welcome, glad our recommendation is helpful :)

      Reply
  4. Vasim Shaikh says:
    Dec 19, 2018 at 8:58 am

    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?

    Reply
    • WPBeginner Support says:
      Dec 19, 2018 at 10:38 am

      You could set it to show if the user is the role of Author

      Reply
  5. Rudolf says:
    Oct 17, 2018 at 12:53 pm

    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.

    Reply
  6. Jonathan P says:
    Feb 7, 2018 at 6:28 am

    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.

    Reply
  7. Kiva says:
    Jan 4, 2018 at 4:01 pm

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

    Reply
  8. Sphelele says:
    Jan 3, 2018 at 3:23 am

    There is no conditional logic feature on my menu.

    Reply
  9. David says:
    Sep 24, 2017 at 11:37 am

    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.

    Reply
  10. Paul says:
    Aug 18, 2017 at 12:15 am

    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!

    Reply
  11. Isuru says:
    Jul 13, 2017 at 12:55 am

    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

    Reply
  12. Mo says:
    May 30, 2017 at 4:48 am

    Yep, definitely broken.

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

    Reply
  13. jban says:
    May 3, 2017 at 5:16 pm

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

    Reply
  14. Jesus Flores says:
    Mar 31, 2016 at 9:13 am

    Works great!!! Thank you

    Reply
  15. Mohi says:
    Dec 1, 2015 at 10:50 pm

    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.

    Reply
    • WPBeginner Support says:
      Dec 1, 2015 at 11:25 pm

      Seems like the plugin is not compatible with your theme’s mega menu functionality. Please contact your theme’s support.

      Reply
  16. JMD says:
    Sep 13, 2015 at 7:15 pm

    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.

    Reply
  17. Peter says:
    Sep 7, 2015 at 9:46 am

    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…

    Reply
  18. Shafiq says:
    Aug 25, 2015 at 10:39 am

    Seems to work ok under WP 4.3.

    Reply
  19. Chuks Eke says:
    May 18, 2015 at 5:20 am

    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

    Reply
  20. Mike J says:
    May 17, 2015 at 10:26 am

    I found this plugin does not work with some themes

    Reply
    • WPBeginner Support says:
      May 17, 2015 at 7:17 pm

      Please report the themes to plugin using the support tab on the plugin page. May be they can help you with this.

      Reply
  21. Dieter says:
    May 17, 2015 at 9:47 am

    Seems to be broken under WP 4.2.2.

    Reply
    • WPBeginner Support says:
      May 17, 2015 at 7:20 pm

      What seems to be the problem? Working fine at our end.

      Reply
      • Dieter says:
        May 18, 2015 at 4:39 am

        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.

        Reply
      • Jagabandhu says:
        Jul 1, 2015 at 2:08 pm

        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.

        Reply
        • WPBeginner Support says:
          Jul 1, 2015 at 3:13 pm

          We just tested the plugin and it is working fine at our demo site. Can you test it by switching to a default theme with no other plugins activated?

Leave a Reply Cancel 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.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
RafflePress - WordPress Giveaway and Contest Plugin
RafflePress
Giveaway and Contest Plugin for WordPress. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2020 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2020)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2020 – Step by Step Guide
Deals & Coupons (view all)
Advanced Coupons
Advanced WooCommerce Coupons
Get 50% off the Advanced Coupons smart coupons plugin for WooCommerce.
ProfilePress
ProfilePress Coupon
Get 20% OFF on ProfilePress WordPress ultimate user and profile plugin.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.