One of the keys to the success of WordPress is probably most The key is the CMS’s willingness to give you the reins and let you do whatever you want with your website. Playing with the controls you can access from your website’s control panel will do wonders. Choosing a WordPress theme can make your website look and behave in amazing new ways. WordPress allows you to create a blog, portfolio or store using the same core system.
Some of the controls you have over your website are not that obvious. When it comes to conditional menus, WordPress doesn’t give you an easy way to add them. It doesn’t matter if the conditional logic is built into the system – there’s no button you can press or a menu you can browse to find a way to add it to your menus.
But that doesn’t mean there isn’t a way to do it. In this article, we will show you:
Conditional logic is the ability to determine an outcome based on the fulfillment of certain criteria. If you’ve heard of an if-then construct, you’ve also heard of conditional logic. So if a certain condition is met, something will happen. If this condition is not met, the action will not be triggered.
Even if you can run a WordPress website without having to learn a single line of code, you should know that it is written in PHP code, just like the themes WordPress uses. To add conditional logic to WordPress, you will rely on conditional tags, small pieces of code whose purpose is to help you display under what conditions what appears on your website.
One of the best examples of how conditional logic can be used in WordPress is forms. A form that is too short may not allow you to capture all the information you need. But each added field makes it more likely that the person filling it out will give up and browse. Finding the middle ground between the two is a very common part of form optimization.
With conditional logic, you can show certain form fields only when certain conditions are met. You can only show fields to visitors who have previously answered a question in a specific way. You can show additional content based on individual choices made by visitors. With some clever use, you can use conditional logic to segment visitors and offer them specific options.
The use case of the menus is not that different. Using conditional logic, you can choose which menu options to show under what conditions. For example, you can show different menu options on different pages. You can also choose to view certain options for different post types. It is also possible to show regular visitors and registrants different options.
Whenever you have doubts about the way to do something that seems complicated in WordPress, you should always check if there is a plugin that can help you do it. Plugins may be better than dealing with sensitive files, especially if you’re not quite sure what you’re doing.
Preferred plugin for adding conditional logic to WordPress menus: Menu – Visibility Control for Menu Items. So your first step would be to install and activate the plugin.
When you’re done, you’ll see a new item in the View menu on your dashboard – the If Menu. Clicking on it will take you to a window where you will find out that you are using a free version of the plugin that only supports basic visibility rules such as those based on user role, user state and visitor device. You need to choose the Premium plan to get advanced rules and third-party integrations.
To see the plugin in action, go to: View > Menus. Select any menu item you want and find the Enable Visibility Rules checkbox.
If you click on the checkbox, you will open a menu that will let you choose the rule (show or hide) and under what conditions the rule should be triggered. Terms are all terms supported by the plugin – user type, user state, page type and device. You will only be able to choose terms that match your subscription plan.
You will be able to put several rules in a single menu item using the AND/OR function, which you can bring up by selecting the small plus sign next to the list of conditions. And that’s all for the If Menu plugin. Don’t forget to save the menu after you finish creating all the rules.
Menu is a great plugin, but even its creator will agree that it only covers a fraction of the possible conditions you can add to your WordPress menu. full list Conditional tags that WordPress supports is extensive and If you find something in it that you would like to add to your website’s menu, you can do so by adding the following code to the theme’s functions.php file:
add_filter('if_menu_conditions', 'additional_menu_conditions'); function additional_menu_conditions($conditions) { $conditions[] = array( 'id' => 'the-post-is-sticky', 'name' => __('The post is sticky'), 'condition' => function($item) { return is_sticky('the-post-is-sticky'); } ); return $conditions; }
With this code we are using the is_sticky tag to allow us to show or hide certain menu items when a visitor is viewing a sticky post. The name “post sticky” is what we will see in the If Menu options.
Be sure to use a child theme when editing functions.php to ensure that the changes you make with edits are permanent and to avoid potential problems.
Let’s wrap it up!
With WordPress, what you can and cannot do with your website is limited only by your knowledge and your willingness to search for a useful plugin or WordPress theme. You can rely on both when adding conditional menus to WordPress. You will find a useful plugin to help you familiarize yourself with the possibilities of conditional logic when used with WordPress menus. But then you’ll learn a few lines of code that will open up many new possibilities for the types of conditions you can set in your menu’s visibility rules.