0

How can I get a user avatar to display in the header of my WordPress site? I've tried:

<a href="<?php echo get_author_posts_url($post->post_author); ?>" title="<?php the_author_meta( 'display_name', $post->post_author ); ?>">
<?php if( get_the_author_meta( 'user_custom_avatar', $post->post_author ) != '' ) { ?>
    <img src="<?php the_author_meta( 'user_custom_avatar', $post->post_author ); ?>" alt="" />
    <?php echo $curauth->display_name; ?>
<?php } else { ?>
    <?php echo get_avatar( get_the_author_meta( 'user_email', $post->post_author ), '80' ); ?>
<?php } ?>
</a>

but every time the logged-in user goes to a different authors post, the avatar changes to that authors avatar. I want it to be that when a user logs in, his/her avatar stays on the top of the page all the time. Can this be done?

1
  • get_avatar just places a default mistery man instead of a loged in user!
    – jimilesku
    Nov 7, 2011 at 21:54

1 Answer 1

5

All you need to do is pass the current users email address into the get_avatar() function.

<?php 

$current_user = wp_get_current_user();

if ( ($current_user instanceof WP_User) ) {
    echo get_avatar( $current_user->user_email, 32 );
}

?>

Here are some links for specifics:

get_avatar(); wp_get_current_user();

A response to your new problem in the comments below:

<?php 

echo '<img src="'. get_the_author_meta( 'user_custom_avatar', $current_user->ID, 32 ) .'" />';

?>

I had one too many semicolons in that there code I gave you before, this ought to work.

EDIT

This will make it 10X easier for you. I dont know why I didnt do it this way to begin with. - I'll just add it into your example snippet the way you want it.

The Real Answer:

<a href="<?php echo get_author_posts_url($post->post_author); ?>" title="<?php the_author_meta( 'display_name', $post->post_author ); ?>">
<?php if( get_the_author_meta( 'user_custom_avatar', $post->post_author ) != '' ) { ?>
    <img src="<?php the_author_meta( 'user_custom_avatar', $post->post_author ); ?>" alt="" />
    <?php echo $curauth->display_name; ?>
<?php } else { 
    $current_user = wp_get_current_user();
    echo get_avatar( $current_user->user_email, $post->post_author ), '80' ); } ?>
</a>

Sorry about that.

6
  • is there any way to get the users image he uploaded? How to incorporate this function in YOur sollution also to have both options: <img src="' . get_the_author_meta( 'user_custom_avatar', $author->author_ID ) . '" alt="" />';
    – jimilesku
    Nov 8, 2011 at 12:01
  • I tried adding echo get_the_author_meta( 'user_custom_avatar', $current_user->ID, 32 ); and it outputs the link to the avatar instead of the custom avatar. I'm stuck. Could YOu please help me out?
    – jimilesku
    Nov 8, 2011 at 12:09
  • Well, if it is outputting the link to the image, just put that on the inside of an img tag in your theme. <img src="<?php echo get_the_author_meta( 'user_custom_avatar', $current_user->ID, 32 ); ?>" /> Nov 8, 2011 at 16:36
  • I don't know where to put the code. It doesn't show anything. If I place it instead the original echo it gives me the error of: unexpected '<'
    – jimilesku
    Nov 8, 2011 at 17:00
  • well You gave me the function but I'm placing the code in header.php , so this is not a valid way because I recieve an error. Oh well...
    – jimilesku
    Nov 8, 2011 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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