2

I want show the avatar user and username with this code.

add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
    foreach ( $menu_items as $menu_item ) {
        if ( strpos($menu_item->title, '#profile_name#') !== false) {
                $menu_item->title =  str_replace("#profile_name#",  wp_get_current_user()->user_login, $menu_item->title);
        }
    }

    return $menu_items;
}

The username is work but i don't known add avatar.

Can you help me please

1

You can use "get_avatar" function to display avatar in menu with user name. Please try code given below:

add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items', 10 );
function my_dynamic_menu_items( $menu_items ) {
    foreach ( $menu_items as $menu_item ) {
        if ( strpos($menu_item->title, '#profile_name#') !== false) {
            $menu_item->title =  str_replace("#profile_name#", wp_get_current_user()->user_login .' '. get_avatar( wp_get_current_user()->user_email, 50), $menu_item->title);
        }
    }
    return $menu_items;
}
0

I wrote a tiny plugin to do just this as I didn't find a nice wordpressy way of doing it. I intend it to work across different themes and with different membership plugins. I have been using Ultimate Member on Divi theme. Feel free to use it or adapt it as required. I hope that you find it helpful: https://wordpress.org/plugins/logged-in-as/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.