My website is built with WordPress and I currently have 3 nav menus
The main menu - "Main"
A secondary top menu - "Player Logged-in"
Another secondary top menu - "Player Logged-out"
Ive added the script below to my functions.php
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = "Player Logged-in";
} else {
$args['menu'] = "Player Logged-out";
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
It is working, the only problem is it replaces my "Main" menu with one of the secondary menus.
How would I go about it if I wanted the 2 secondary top menus to alternate if a user is logged in/out AND I wanted the "Main" menu to stay regardless of whether the user is logged in or not?
Thanks
I came right with this problem.
I looked for the specific menu locations (top_navigation) for my theme (Avada) and used this
function wpc_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in()) {
if( 'top_navigation' == $args['theme_location'] ) {
//top_navigation is specific to Avada in my case
$args['menu'] = 'Player Logged-in';
}
} else {
if( 'top_navigation' == $args['theme_location'] ) {
//again, top_navigation is specific to Avada in my case
$args['menu'] = 'Player Logged-out';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );
Related
I've got a script that works.... a bit too well. It's supposed to be when you login to the site, the main nav reflects that switch. It works, but it's only supposed to occur on the nav in the header. Instead, it changes all nav elements, no matter where they are. So I have three other menus in the footer but they all change to the same menu.
How can I just target the main nav in my header alone?
Here's the code:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
// Logged in menu to display
$args['menu'] = 80;
} else {
// Non-logged-in menu to display
$args['menu'] = 25;
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
If you check the docs for that filter you will see that the $args that come have an index 'theme_location' where you can indicate, precisely in which menus your code should run.
You need the identifier for that menu, which can be something like 'primary' or 'main_menu' - whatever the theme author(s) decided to identify the theme location for the menu.
Log/check the $args variable to obtain the ones available in your theme, and identify the one you need.
So, if your header nav's id is, 'primary', your snippet should be updated to this, to check that it only runs for that menu and leaves the others alone:
function my_wp_nav_menu_args( $args = '' ) {
//If it is not the header nav menu, don't do anything
if( $args['theme_location'] !== 'primary' ){
return $args;
}
if( is_user_logged_in() ) {
// Logged in menu to display
$args['menu'] = 80;
} else {
// Non-logged-in menu to display
$args['menu'] = 25;
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
This should work but let me know if you need more help.
the question is just on the title, but well here's the problem i tried this code on my function.php which at first works fine, but suddenly i realized that the menus on footer changed to be the same as the header menus.
here is the code :
function wpc_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-out';
} else {
$args['menu'] = 'Main Menu';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );
What i want is to have a different header menus for user when they log in and log out, but the footer menus stays the same. i am currently learning how to add the conditional logic on the functions.php atm, but if there are other ways please let me know, thx in advance
Probably a bit late for a reply but I just did this in the Astra theme today, so thought I would share the know-how for others who might need it. It can easily be adjusted for any WordPress theme actually:
You can check the location of the menu and only change the menu in the location(s) you decide (in this example 'primary' and 'mobile_menu' which is what Astra theme calls the the main nav locations for desktop and mobile) :
function custom_wp_nav_menu_args( $args = '' ) {
if( $args['theme_location'] == 'primary' || $args['theme_location'] == 'mobile_menu' ) {
if( ! is_user_logged_in() ) {
$args['menu'] = 'logged-out';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_wp_nav_menu_args' );
If you dont want to use the explicit position of the menu (in my case the nav was in a widget in the footer and not a set theme location), you can also do the same thing by using the id, name or slug of the menu. For example if you want to change the menu 'test-menu' for 'test-menu-logged-out' if the user is logged out, you can use this:
function custom_2_wp_nav_menu_args ( $args = '' ) {
$menu_slug = $args['menu']->slug;
if ( ! is_user_logged_in() && $menu_slug == 'test-menu' ){
$args['menu'] = 'test-menu-logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_2_wp_nav_menu_args' );
i am new to WordPress and trying to find a way to add menus based on user roles. I have (guest user or non logged in user) vendor and subscriber roles. I want to display different primary menus depending on what role user is. Example code taken from https://wpcodeus.com/display-different-wordpress-menu-to-logged-in-users/
Example code not working
function nav_menu_args( $args = '' ) {
if( is_user_logged_in('vendor')) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'VendorMenu';
}
}
else if
( is_user_logged_in('subscriber')) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'SubscriberMenu';
}
}
else
(!is_user_logged_in) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'PrimaryMenu';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'nav_menu_args' );
At the moment it changes all menus to vendor menu including primary top and footer, all menus have the same menu.
Any help or guidance in the right direction will be much appreciated.
I am adding the code in to the custom/child theme functions.php file.
you can use current_user_can() function to check user capabiltiy or role. see this link.
This worked for me (slightly different method but same outcome)
if ( current_user_can( 'subscriber' ) ) {
//display menu
}
if ( current_user_can( 'vendor' ) ) {
//display menu
}
I am trying to use a custom menu on my landingpage.
I used this code:
<?php
function change_wp_nav_menu_args($args = ''){
$pageID = get_the_id();
if($pageID == '63') //custom menu for site with id 63
{
$args['menu'] = 'homepage';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'change_wp_nav_menu_args' );
?>
It works fine, but not only the main menu changes.
The footer menu changes also.
But the footer menu should be the same on every page.
How can I affect this?
This code fixed my problem:
<?php
add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );
function bb_wp_nav_menu_args( $args = '' ) {
// change the menu in the Header menu position
if( $args['theme_location'] == 'primary' && is_page('63') ) {
$args['menu'] = '6'; // 32 is the ID of the menu we want to use here
}
return $args;
}
?>
Its great that you find your answer, but it's a suggestion to make the code dynamic rather set the menu value as static [ $args['menu']='6' ].
Suggestion :
Create a Meta Box [ Dropdown with list of menu ] for Page with the Label Menu. And use the Menu Id for the wp_nav_menu .
For Dropdown [ listing Menus ]
function your_menus()
{
$menu_arr=NULL;
$menus=get_terms( 'nav_menu', array( 'hide_empty' => true ) );
$menu_arr['your-nomenu']='Default';
foreach ( $menus as $menu ){
$menu_arr[$menu->slug]=$menu->name;
}
return $menu_arr;
}//end of function
For Nav Menus : [ _your_page_menu : meta name ]. You can place the code inside a function and called it in Header or you can place this code directly into the header.
$page_menu_name=get_post_meta(get_the_ID(),'_your_page_menu',true)==''?'your-nomenu':get_post_meta(get_the_ID(),'_your_page_menu',true);
if($page_menu_name==='your-nomenu')
{
wp_nav_menu(array('theme_location' => 'primary','menu_id'=> 'main-menu','container'=>false,'fallback_cb'=>'','menu_class'=>'main-navigation'));
}
else
{
wp_nav_menu(array('menu_id'=> 'main-menu' , 'container'=>false, 'menu'=>$page_
menu_name,'fallback_cb'=>'','menu_class'=>'main-navigation'));
}
Hope it helps you.
I want to change menu label of items accordingly if a user is logged in or not .
If user is not logged in i want to show Login and if user is admin I want to show Admin page and if normal user is logged in i want to show My Profile page .
I have tried this code.
add_filter( 'wp_nav_menu_items', 'dynamic_label_change', 10, 2 );
function dynamic_label_change( $items, $args )
{
if (!is_user_logged_in() && $args->theme_location == 'topbar_navigation')
{
$items = str_replace("Login", "Profile", $items);
}
return $items;
}
If you want to add new menu item then below code will help.
// Add Login / Logout menu item dynamically to primary navigation menu
function custom_menu_links( $items, $args ) {
if ($args->theme_location == 'primary'){
if (is_user_logged_in()) {
$items .= '<li>Logout</li>';
} else {
$items .= '<li>Login</li>';
}
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'custom_menu_links', 10, 2 );