• Resolved ianharris02

    (@ianharris02)


    Hello,

    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 ““;

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

    Thank you for your time,
    Ian Harris

    • This topic was modified 3 years, 2 months ago by ianharris02.
Viewing 1 replies (of 1 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.

    • This reply was modified 3 years, 2 months ago by Mahdi.
    • This reply was modified 3 years, 2 months ago by Mahdi.
Viewing 1 replies (of 1 total)
  • The topic ‘Display Author Avatar Picture in Header’ is closed to new replies.