• Resolved ianharris02

    (@ianharris02)


    Hello,

    I sent a previous thread, titled “Display Author Avatar Picture in Header,” but for some reason, the code wrapper didn’t trigger, so this is my most recent thread. The other can be disregarded.

    I am building an elementor site and I am trying to use the script below that I found in the directorist forum, but when placed in the header, it only works when I am on the dashboard page (provided by your plugin). When I am on any other page, this code snippet doesn’t load the image, it just displays a broken image icon instead. Is there a way to adapt the code below so that the avatar image is able to be displayed in the header on any page?

    $image_id = get_user_meta( get_the_author_meta( ‘ID’ ), ‘pro_pic’, true );
         $image = $image_id ? wp_get_attachment_image_src(
         $image_id, ‘thumbnail’ ) : ”;
         $image_url = is_array( $image ) ? $image[0] : $image;
         echo “<img src='$image_url' alt=''>";

    Also, is there a way to input a default image if a user is not logged in?

    Thank you for your time,
    Ian Harris

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi Ian,

    Could you try this code instead:

    
    $author_id = get_user_meta( get_current_user_id(), 'pro_pic', true );
    $author_img = wp_get_attachment_image_src( $author_id ); //author avatar
    $avatar_img = get_avatar( get_current_user_id(), 40, null, null); //default avatar
    if ( empty( $author_img ) ) {
    
    echo wp_kses_post( $avatar_img );
    
    } else {
    echo sprintf( '<img width="40" src="%s"/>', esc_url( $author_img[0] ) );
    }
    

    Let me know if it doesn’t work.

    Thread Starter ianharris02

    (@ianharris02)

    Thank you so much; worked like a charm!

    Where would this code be entered…header.php?

    Hi @buckyhenson,

    This was in the header.php If I recall correctly. But depends, if your Theme handles things differently, it would be on a different file.

    Regards,
    Mahdi.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display Author Avatar in Header’ is closed to new replies.