functions - Show some menu sub items as dropdown under a menu item

I have made a custom wordpress menu - function my_custom_menu_item($items, $args){if(is_user_logged_in() && $ar

I have made a custom wordpress menu -

function my_custom_menu_item($items, $args)
{
    if(is_user_logged_in() && $args->theme_location == 'primary')
    {
        $user=wp_get_current_user();
        $name=$user->display_name; 
        $items .= '<li><a href="">'.$name.'</a></li>';

        $items .= '<li><a href="/my-profile">My Profile</a></li>';
        $items .= '<li><a href="/my-posts">My Posts</a></li>';
    }
    return $items;
}
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2);

It shows 'name of the logged in user', 'My Profile', 'My Posts' side by side in a flat structutre.

I want 'My Profile' and 'My Posts' to show as dropdown under 'name of the logged in user'. How do I do that?

I have made a custom wordpress menu -

function my_custom_menu_item($items, $args)
{
    if(is_user_logged_in() && $args->theme_location == 'primary')
    {
        $user=wp_get_current_user();
        $name=$user->display_name; 
        $items .= '<li><a href="">'.$name.'</a></li>';

        $items .= '<li><a href="/my-profile">My Profile</a></li>';
        $items .= '<li><a href="/my-posts">My Posts</a></li>';
    }
    return $items;
}
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2);

It shows 'name of the logged in user', 'My Profile', 'My Posts' side by side in a flat structutre.

I want 'My Profile' and 'My Posts' to show as dropdown under 'name of the logged in user'. How do I do that?

Share Improve this question asked Oct 2, 2019 at 17:10 Gissipi_453Gissipi_453 1053 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

Remove the first closing </li> and wrap the sub-items inside <ul></ul>, then close the parent <li>.

For Example:

function my_custom_menu_item($items, $args)
{
    if(is_user_logged_in() && $args->theme_location == 'primary')
    {
        $user=wp_get_current_user();
        $name=$user->display_name; 
        $items .= '<li><a href="">'.$name.'</a>';
        $items .= '<ul>';
        $items .= '<li><a href="/my-profile">My Profile</a></li>';
        $items .= '<li><a href="/my-posts">My Posts</a></li>';
        $items .= '<ul>';
        $items .= '</li>';
    }
    return $items;
}
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745111852a4611896.html

相关推荐

  • functions - Show some menu sub items as dropdown under a menu item

    I have made a custom wordpress menu - function my_custom_menu_item($items, $args){if(is_user_logged_in() && $ar

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信