Ottenere l'avatar dell'utente loggato in WordPress
Come posso ottenere l'avatar di un utente da visualizzare nell'header del mio sito WordPress? Ho provato:
<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="Avatar utente" />
<?php echo $curauth->display_name; ?>
<?php } else { ?>
<?php echo get_avatar( get_the_author_meta( 'user_email', $post->post_author ), '80' ); ?>
<?php } ?>
</a>
ma ogni volta che l'utente loggato visita un post di un altro autore, l'avatar cambia con quello dell'autore del post. Vorrei che quando un utente effettua il login, il suo avatar rimanga sempre in cima alla pagina. È possibile farlo?
Tutto ciò che devi fare è passare l'indirizzo email dell'utente corrente nella funzione get_avatar().
<?php
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) {
echo get_avatar( $current_user->user_email, 32 );
}
?>
Ecco alcuni link per i dettagli:
get_avatar(); wp_get_current_user();
Una risposta al tuo nuovo problema nei commenti qui sotto:
<?php
echo '<img src="'. get_the_author_meta( 'user_custom_avatar', $current_user->ID, 32 ) .'" />';
?>
Avevo un punto e virgola di troppo nel codice che ti ho dato prima, questo dovrebbe funzionare.
MODIFICA
Questo renderà tutto 10 volte più semplice per te. Non so perché non l'abbia fatto così fin dall'inizio. - Lo aggiungerò semplicemente nel tuo snippet di esempio nel modo in cui lo vuoi.
La vera risposta:
<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="Avatar dell'autore" title="Avatar personalizzato dell'autore" />
<?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>
Mi dispiace per questo.

esiste un modo per ottenere l'immagine caricata dall'utente? Come incorporare questa funzione nella tua soluzione per avere entrambe le opzioni: <img src="' . get_the_author_meta( 'user_custom_avatar', $author->author_ID ) . '" alt="" />';

ho provato ad aggiungere echo get_the_author_meta( 'user_custom_avatar', $current_user->ID, 32 ); e restituisce il link all'avatar invece dell'avatar personalizzato. Sono bloccato. Potresti aiutarmi?

beh, se restituisce il link all'immagine, basta inserirlo all'interno di un tag img nel tuo tema. <img src="<?php echo get_the_author_meta( 'user_custom_avatar', $current_user->ID, 32 ); ?>" />

Non so dove inserire il codice. Non mostra nulla. Se lo metto al posto dell'originale echo mi dà l'errore: unexpected '<'

Beh, mi hai dato la funzione ma sto inserendo il codice in header.php, quindi questo non è un modo valido perché ricevo un errore. Oh beh...

Ecco la risposta corretta di Drew Gourley senza l'errore:
<a href="<?php echo esc_url(get_author_posts_url($post->post_author)); ?>" title="<?php echo esc_attr(get_the_author_meta('display_name', $post->post_author)); ?>">
<?php if (get_the_author_meta('user_custom_avatar', $post->post_author) != '') { ?>
<img src="<?php echo esc_url(get_the_author_meta('user_custom_avatar', $post->post_author)); ?>" alt="<?php echo esc_attr(get_the_author_meta('display_name', $post->post_author)); ?>" />
<?php echo esc_html(get_the_author_meta('display_name', $post->post_author)); ?>
<?php } else {
$current_user = wp_get_current_user();
echo get_avatar($current_user->user_email, 80); // Correzione della sintassi rimuovendo l'uso non necessario di `$post->post_author` dentro `get_avatar`
} ?>
</a>
