Show Menu Items with Conditional Logic (2 Methods) with and without Plugin

If you want to change menu items with conditional logic in WordPress or just want to show different menu items according to the role of the user then read this tutorial and follow step by step.

Before we move on to the actual tutorial to change menu items with conditional logic in WordPress, let’s have a look what are the benefits and use cases of changing menu items with Conditional Logic in WordPress.

Why use Conditional Logic to Change Menu Items in WordPress?

Let me share some use cases:

  1. Membership Website: If you’re creating a membership website, you’ll need to show different menu items according to the role of the user. You’ll need to change menu items with Conditional Logic in WordPress to show different menu items to different user roles.
  2. Educational Website: If you’re building an online education portal with WordPress with student signup system, you’ll need to change menu items as per the student details. Like for non-logged-in students, you’ll show menu items like log in or Signup links.
  3. Page Specific Menu Items: You can also change menu items according to a specific page on your WordPress website.

There are two methods to change menu items on your WordPress website.

Method 1: Change Menu Items on WordPress website using If Menu Plugin

If you want to change Menu Items with Conditional Logic in WordPress without coding, then you can use the If Menu plugin.

  1. Install and Activate If Menu Plugin: Go to Plugins>Add New section and search for If Menu plugin or go to this link to download If Menu and install the If Menu plugin. After installing theIf_Menu.PNG
  2. Go to Menu Option: Open WordPress Dashboard and then hover over Appearance and navigate to the Menus section.If_Menu_Menu.PNG
  3. Edit Menu Items: In the Menus section, you need to edit a menu and open the editing panel of a particular menu item by clicking on the drop-down icon.If_Menu_Choose_Menu_Item.PNG
  4. Enable Visibility Rules: In the Menu item editing section, you need to tick the box to Enable Visibility Rules. Check the screenshot below for reference.If_Menu_Enable_Visibility_Rules.PNG
  5. Choose Visibility Rules: In the Menu item editing section, you need to choose a particular condition from the following conditions. Examples of Conditions available:image.pngIf_Menu_Choose_Visibility_Rules.PNG
  6. Save Your Menu: Once you’re done with the conditions in menu items, you can save your menu and display it on the header, sidebar wherever you want.If_Menu_Save_Menu.PNG

Method #2: Change Menu Items on WordPress website Without Plugin

Follow the steps carefully:

  1. Create Two different Menus for your website
  2. Navigate to Appearance>Theme Editor>functions.php
  3. Paste the following code in functions.php and replace the Menu Name.
//This functions allows you to switch menus depending on if a user is logged into your site or not and, if they are logged in, what they're user role is

function my_conditional_menus( $args = '' ) {
 [
if( is_user_logged_in() && current_user_can('administrator') ) { 
    $args['menu'] = 'REPLACE-WITH-LOGGED-IN-ADMIN-MENU-NAME'; //this menu will be shown ONLY to logged in admins
} elseif ( is_user_logged_in() && !current_user_can('administrator') ) { 
    $args['menu'] = 'REPLACE-WITH-LOGGED-IN-NON-ADMIN-MENU-NAME'; //this menu will be shown to any logged in user that is NOT a site admin
	show_admin_bar(false); //removes the WP admin bar from the front end of the website
} else {
	$args['menu'] = 'REPLACE-WITH-MAIN-MENU-NAME'; //this menu will be shown to all logged out users
}
    return $args;
}
add_filter( 'wp_nav_menu_args', 'my_conditional_menus' );

Conclusion

Follow the step by step tutorial without missing anything, otherwise, it won’t work.

Peace out!

Leave a Comment