Home >CMS Tutorial >WordPress >About showing different WordPress menu for logged in users

About showing different WordPress menu for logged in users

藏色散人
藏色散人forward
2021-02-02 15:28:292152browse

The following column WordPress Tutorial will introduce to you how to display different WordPress menus for logged in users. I hope it will be helpful to friends in need!

If you want logged-in users and non-logged-in browsers to display different menus, you can use the following code:

Login users display different WordPress menus Login users display different WordPress menus Menu

Add the following code to the current theme function template functions.php:

if( is_user_logged_in() ) { 
$args['menu'] = 'logged-in';
} else { 
$args['menu'] = 'logged-out';
} 
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );

Then create two new menus, logged-in and logged-out, for login and normal browsing. or the menu displayed.

If the theme has multiple menus, you can use the following code to display different menus at the specified menu location:

function wpc_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in()) { 
if( 'top-navigation' == $args['theme_location'] ) { // Change top-navigation to theme specific name
$args['menu'] = 'logged-in';
}
} else { 
if( 'top-navigation' == $args['theme_location'] ) { // Change top-navigation to theme specific name
$args['menu'] = 'logged-out';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );

You can also use the above method to allow different user roles to display different menus content.

Original code: https://wpcodeus.com/display-different-wordpress-menu-to-logged-in-users/

The above is the detailed content of About showing different WordPress menu for logged in users. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:zmingcx.com. If there is any infringement, please contact admin@php.cn delete