I have two menus namely : "nav-menu" and "user-menu". I just want to switch (or toggle) to a specific menu between the two menus to be primary menu whenever I visit a particular page.
For ex : If I go to some particular page then my menu programatically switches to "user-menu".
I have visited How to switch between the Primary Menus programmatically?. However, I am still not able to figure it out.
Can someone help me by giving brief explanation of the code?
I have two menus namely : "nav-menu" and "user-menu". I just want to switch (or toggle) to a specific menu between the two menus to be primary menu whenever I visit a particular page.
For ex : If I go to some particular page then my menu programatically switches to "user-menu".
I have visited How to switch between the Primary Menus programmatically?. However, I am still not able to figure it out.
Can someone help me by giving brief explanation of the code?
Share Improve this question asked Aug 14, 2019 at 21:32 noobronnoobron 12 Answers
Reset to default 0You can try this code in specific page template
function change_primary_menu( $menu ) {
// you can check spesific menu id using this print and after that assign in,
// and you can also check menu location using this print too
echo '<pre>';
print_r($menu) ;
echo '</pre>';
$user = wp_get_current_user();
//in this example im check the user role is 'user'
if ( in_array( 'user', (array) $user->roles ) ) {
//if yes change to user menu (19 and 21 is just example menu id)
$menu['primary'] = 19;
}else{
//if not change to another menu
$menu['primary'] = 21;
}
return $menu;
};
add_filter( 'theme_mod_nav_menu_locations', 'change_primary_menu', 10, 2 );
I got this worked out! I used 'wp_nav_menu_items' hook to customize the links of the menu. However, it actually dosen't specifically answers my question but it solves my problem. Here's an example for the 'PROFILE' page :-
add_filter( 'wp_nav_menu_items', 'set_profile_links', 10, 2 );
function set_profile_links( $items, $args ) {
$items = '<li ><a href="http://localhost/wordpress/index.php/dashboard">Dashboard</a></li>';
$items .= '<li id="current-page-menu"><a href="http://localhost/wordpress/index.php/my-account">PROFILE</a></li>';
return $items;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745240515a4618154.html
评论列表(0条)