Conditional rules for menus are used to program menu’s or menu items’ to behave in a certain way dependent on a combination of factors. Such rules can for example help you display different menus on different pages or hide or show individual menu items, depending on who is viewing them or where on the site the visitor is.
Such conditional rules for menus aren’t a feature that all webmasters need, simply because the basic menu functionality of WordPress in combination with most themes is enough to satisfy their needs.
WordPress provides advanced and flexible menu building functionality and the option to build multiple menus out of the box. Combined with most themes’ feature to use different menus in different locations or – in the case of more advanced themes – to choose a different menu for each page using microsites or widgetized sidebars, it enables you to fully customize the navigation of your site.
However, applying conditional rules to menus may be necessary in cases, where multiple pages share almost the same menu structure with the exception of an item or two. As by building individual menus for each of these pages you might end up with 20+ menus that are difficult to manage, using conditions to control the behavior of menu items can be a really good idea.
Another example, where conditional logic is necessary, is when you need to show different content, including menus, to different user roles or to logged in and anonymous users.
How to Apply Conditional Rules to Menus?
There are two ways to implement conditional logic: by writing the necessary functions yourself or by using a plugin.
This post will present you to the option to implement conditional logic via a plugin, as this is the easiest and most common way to create conditional menus.
There are two types of plugins out there: such helping you switch whole menus in a given spot based on conditions and such helping you swap, hide or show individual menu items depending on provided conditions.
If you are looking for a plugin that can help you swap menus depending on different conditions, you should use a plugin as Conditional Menus by Themify. This open source plugin if one of the few out there allowing you to select, which menus should be eligible for swap for each menu spot on the site and what conditions should be met in order for a specific menu to be displayed.
You can set up Conditional Menus in four simple steps:
- Create all the menus that you want to use from Appearance->Menus->Add New
- Choose which menus should be available for swap for each menu holder
- Click on +Conditions to the right to define, when the different menus should be displayed.
- Click on Save Changes. That’s it.
Applying Conditional Rules to Menu Items
If you are looking for a plugin that can enable you to manipulate the behavior of individual menu items based on conditions, such as user role, logged in vs. anonymous user, page on which the menu appears or a combination of some of these factors, you can choose one of the many available plugins.
If you are looking for recommendation, here it comes: After a careful evaluation of the available options, WP Blogging Nerd recommends the use of If Menu.
If Menu has the following advantages:
- Is updated regularly (meaning that you can expect future bug fixes and feature updates)
- Has 30 000+ active installs (popular plugins are less likely to be deprecated)
- Is absolutely free
- Has high rating, 4.5 out of 5
- Is the most powerful plugin for applying conditional rules to menus in WordPress (while other plugins allow you to only control menu item behavior based on a single condition (such as user role or page positioning, If Menu allows you to use all conditions listed above and their combinations).
In order to use If Menu to apply conditional rules to menu items, you have to install the plugin, activate it, and set it up. Here’s how you can do it, step-by-step:
- Log into WordPress
- Create the menus you’d like to apply conditional logic to
- Go to Plugins->Add New from within WordPress admin panel
- Enter If menu in the search box to the right
- Choose If Menu and click on Install Now
- Once installed, click on Activate.
- Enable visibility rules for menu items on Appearance->Menus
- Choose a menu to edit
- Navigate to an item you want to apply conditional rules to and click on it to expand it
- Choose the appropriate combination of rules and functions
- Once you have edited all menu items, click on Save menu. That’s it.