1

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

0

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;
}
|improve this answer|||||

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.