Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022 Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022 Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022
Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022 Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022 Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022

Get Qi Theme with 150 superbly designed WordPress demos

Get Qi Theme
Qi Theme
Sign up for our newsletter and be the first to get all the latest Qode news and updates.
BACK TO TOP

How to Add Conditional Logic to Your WordPress Menus

How to Add Conditional Logic to Your WordPress Menus

One of the keys to WordPress’ success, possibly even the key, is the CMS’s willingness to give you the reins and let you do whatever you want with your website. Simply playing around with the controls you can access from your website’s dashboard will do wonders. Choosing a WordPress theme can make your website look and behave in awesome new ways. WordPress lets you build a blog, a portfolio, or a store, using the same core system.

Some of the controls you have over your website aren’t that obvious. When it comes to conditional menus, WordPress won’t give you an easy way to add them. It doesn’t matter that the conditional logic is built into the system — there isn’t a button you can push or a menu you can browse to find a way to add it to your menus.

But that doesn’t mean there’s no way to do it. In this article, we’ll show you:

What Is Conditional Logic?

What Is Conditional Logic

Conditional logic is the ability to set an outcome based on the fulfillment of specific criteria. If you’ve ever heard of an if-then construct, you’ve heard of conditional logic. So if a particular condition is met, something is going to happen. If that condition isn’t met, the action won’t be triggered.

Even though you can run a WordPress website without ever having to learn a single line of code, you should still know that WordPress is written with PHP code, as are the themes it uses. To add conditional logic to WordPress, you’ll rely on conditional tags, small pieces of code whose purpose is to help you display what appears on your website under which circumstances.

Qode Themes: Top Picks
Bridge New Banner
Bridge

Creative Multi-Purpose WordPress Theme

Stockholm WordPress Theme
Stockholm

A Genuinely Multi-Concept Theme

Startit WordPress Theme
Startit

Fresh Startup Business Theme

What Can You Use Conditional Logic For?

What Can You Use Conditional Logic For?

One of the best examples of how conditional logic can be put to use in WordPress is with forms. A form that’s too short might not allow you to capture all the information you need. But every added field makes it more likely that the person filling it out will give up and browse away. Finding the middle ground between the two is an all too common part of form optimization.

With conditional logic, you can show certain form fields only when specific conditions are met. You can show the fields only to the visitors who have answered an earlier question in a certain way. You can show additional content based on individual choices the visitors have made. With some clever use, you can use conditional logic to segment the visitors and provide them specific options.

The use case for menus isn’t that different. Using conditional logic, you can choose which menu options show under which conditions. You can show different menu options on different pages, for example. You can also choose to display specific options for different post types. And it’s also possible to show different options to regular visitors and those who are logged in.

How to Add Conditional Logic to Menus with a Plugin

Whenever you have doubts about the way to do something seemingly complicated in WordPress, you should always check if there’s a plugin that helps you do it. Plugins can be better than tinkering with sensitive files, especially if you’re not exactly sure what you’re doing.

The plugin of choice for adding conditional logic to WordPress menus is If Menu – Visibility Control for Menu Items. So your first step would be to install the plugin and activate it.

If Menu

After you’re done, you’ll notice a new item in the Appearance menu in your dashboard – If Menu. Clicking on it will take you to a window where you’ll find out that you’re using a free version of the plugin that supports only basic visibility rules such as those conditional on user role, user state, and visitor device. To get advanced rules and third-party integrations, you’ll have to opt for the Premium plan.

Conditional Menus

To see the plugin in action, navigate to Appearance > Menus. Select any menu item you want and find the Enable Visibility Rules checkbox.

Conditional Menus

If you click on the checkbox, you’ll bring up a menu that will allow you to choose the rule — either show or hide — and the conditions under which the rule will be triggered. The conditions are all the ones supported by the plugin — type of user, user state, page type, and device. You’ll only be able to choose the conditions that match your subscription plan.

Conditional Menus

You’ll be able to set several rules to a single menu item using the AND/OR function you can bring up by selecting the little plus sign next to the list of conditions. And that’s all there is to the If Menu plugin. Remember to save the menu once you’re done creating all the rules.

How to Add Custom Conditional Logic to WordPress Menus

If Menu is a great plugin, but even its creator will acknowledge that it only covers a fraction of possible conditions you could add to your WordPress menu. The full list of conditional tags WordPress supports is extensive, and if you find in it something you’d want to add to your website’s menu, you can do it 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’re using the is_sticky tag to let us show or hide certain menu items from a visitor while they’re viewing a sticky post. The name “The post is sticky” is what we’ll see in the If Menu options.

Conditional Menus

To be sure that the changes you make with the edits stick, and to avoid potential issues, make sure you use a child theme when editing functions.php.

Let’s Wrap It Up!

With WordPress, what you can’t and cannot do with your website is only limited by your knowledge and willingness to search for a useful plugin or WordPress theme. When adding conditional menus to WordPress, you can rely on both. You’ll find the useful plugin that will help you get acquainted with the possibilities of conditional logic when put into use with WordPress menus. But then, you’ll learn a couple of lines of code that will open up lots of new possibilities for the types of conditions you can set in your menu’s visibility rules.

Post your comment

Comments0