1

I am attempting to add an avatar image to the top nav menu. I am currently using this function, which takes a custom link and replaces it with the currently logged in user's name. That part of the function works excellent.

My attempts to modify the function haven't gotten me across the finish line yet. Yes, there is a question on here that is kind of similiar, Inserting PHP before a menu element, while using the inbuilt menu function wp_nav_menu(), and I've read it, but there isn't really enough explanation on it to help me with my setup.

Here's the modified function from my functions.php file:

function my_custom_menu_item($items) {
$remove_childs_of = array();

if($item->title == "##currentuser##") {
    if(is_user_logged_in()) {
        $user=wp_get_current_user();
        $size = 'thumbnail';
        $imgURL='';

        if (function_exists('get_wpupa_url'))  
            $imgURL = get_wpupa_url($user->id, ['size' => $size]);

        echo "<img class='avatar-menu' src='" . $imgURL ."'>";  

        $name=$user->display_name; // or user_login , user_firstname, user_lastname
        $items[$index]->title = $name;
    }
    else {
        array_push($remove_childs_of, $item->ID);
        unset($items[$index]);
    }
}
if(!empty($remove_childs_of) && in_array($item->menu_item_parent, $remove_childs_of)) {
    array_push($remove_childs_of, $item->ID);
    unset($items[$index]);
}

return $items;
}
?>

While this does technically work, since I'm not actually inserting the image into the array of the function $items[$index] , that's perhaps where I'm going wrong?

Thanks for any suggestions you fine folks might have. :)

0

Your Answer

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

Browse other questions tagged or ask your own question.