在WordPress中获取登录用户的头像

时间:2011-11-07 作者:jimilesku

如何让用户头像显示在WordPress站点的标题中?我尝试过:

<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>
但是,每次登录的用户转到不同的作者帖子时,化身都会更改为该作者的化身。我希望当用户登录时,他/她的头像始终保持在页面顶部。这能做到吗?

1 个回复
最合适的回答,由SO网友:Drew Gourley 整理而成

您只需将当前用户的电子邮件地址传递到get\\u avatar()函数中。

<?php 

$current_user = wp_get_current_user();

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

?>
以下是一些具体链接:

get_avatar();wp_get_current_user();

以下是对您新问题的回应:

<?php 

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

?>
我之前给你的代码中分号太多了,这应该行得通。

编辑

这将使您的操作更加轻松。我不知道为什么我一开始没有这样做我将按您所希望的方式将其添加到您的示例片段中。

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>
对此表示抱歉。

结束

相关推荐

是否从_Author_meta中删除“http://”?

默认情况下,WordPress在用户在其配置文件中添加的URL前面打印http://<?php the_author_meta(\'user_url\'); ?> 有没有人能给我提供默认情况下去掉这些URL“HTTP://”的代码?(虽然仍然是一个链接,不接触WP的后端…我想这可能是一个功能?)