Ottenere l'avatar dell'utente loggato in WordPress

7 nov 2011, 23:45:52
Visualizzazioni: 13.8K
Voti: 0

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?

1
Commenti

get_avatar inserisce solo un misterioso uomo predefinito invece dell'utente loggato!

jimilesku jimilesku
7 nov 2011 23:54:47
Tutte le risposte alla domanda 2
6

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.

8 nov 2011 01:03:16
Commenti

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="" />';

jimilesku jimilesku
8 nov 2011 14:01:52

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?

jimilesku jimilesku
8 nov 2011 14:09:38

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 ); ?>" />

Drew Gourley Drew Gourley
8 nov 2011 18:36:30

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

jimilesku jimilesku
8 nov 2011 19:00:37

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...

jimilesku jimilesku
8 nov 2011 22:54:33

Questo perché ho fatto confusione, è difficile inserire codice in questi dannati commenti. Vedi il secondo blocco di commenti aggiunto alla mia risposta.

Drew Gourley Drew Gourley
9 nov 2011 00:05:59
Mostra i restanti 1 commenti
0

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>
7 ott 2024 18:07:01