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
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:
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.
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+.
A small feature, but very missed when a programmer is asked to work in wordpress.
Glad you found this recommendation helpful
Thanks for this, it really helped
You’re welcome, glad our guide helped
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.
You’re welcome, glad our recommendation is helpful
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?
You could set it to show if the user is the role of Author
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.
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.
Could I set up this plugin so that I can set up different menu options for each different category?
There is no conditional logic feature on my menu.
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.
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!
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
Yep, definitely broken.
Works great for the built in conditions, but when coding custom conditions it works once and then stops working.
YES!!! After 90 minutes of trying to figure this out, this article made it work for me. THANK YOU!!!
Works great!!! Thank you
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.
Seems like the plugin is not compatible with your theme’s mega menu functionality. Please contact your theme’s support.
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.
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…
Seems to work ok under WP 4.3.
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 found this plugin does not work with some themes
Please report the themes to plugin using the support tab on the plugin page. May be they can help you with this.
Seems to be broken under WP 4.2.2.
What seems to be the problem? Working fine at our end.
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.
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?