php - How to switch between two primary menus (programatically0

I have two menus namely : "nav-menu" and "user-menu". I just want to switch (or toggle) to a specifi

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 1
Add a comment  | 

2 Answers 2

Reset to default 0

You 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

相关推荐

  • php - How to switch between two primary menus (programatically0

    I have two menus namely : "nav-menu" and "user-menu". I just want to switch (or toggle) to a specifi

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信