admin管理员组文章数量:1415645
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;
}
本文标签: phpHow to switch between two primary menus (programatically0
版权声明:本文标题:php - How to switch between two primary menus (programatically0 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745240514a2649300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论