将 Gravatar 添加到 WP 标头


Adding Gravatar to WP header

我想显示当前登录用户的头像,如果用户没有头像,它将显示默认的wp man(或使用URL的img)

这是我目前拥有的代码;

<div class="gravatar">
<img src="http://techyoucation.com/wp-content/themes/blank/images/treehouse.png" alt=""/> 
</div>

上面的代码只显示了一个随机图像,我想将其更改为登录用户的头像(当没有用户登录时,它会隐藏,但我稍后会这样做)。

那么有谁知道我需要做什么才能让它工作?

提前致谢

阿莱德

更新

<div class="logo"><a href="http://techyoucation.com"><img src="<?php
$user_id = get_current_user_id() // Get the user ID
$size_avatar = 50 // Set the avatar size
$default_avatar = 'http://techyoucation.com/wp-    content/themes/blank/images/treehouse.png' // Set your default image url
echo get_avatar( $user_id, $size_avatar, $default_avatar );
?>" alt="techyoucation logo" width="219" height="47" id="Logo" /></a></a>

Dreamweaver 说有 3 个语法错误?我用错了你的代码吗?

谢谢

更新

好的,现在我有了;

<div class="gravatar">
<img src="<?php
$user_id = get_current_user_id(); // Get the user ID
$size_avatar = 50; // Set the avatar size
$default_avatar = 'http://techyoucation.com/wp-content/themes/blank/images/treehouse.png'; // Set your default image url
echo get_avatar( $user_id, $size_avatar, $default_avatar );
?>" alt=""/> 
  </div>

但图像不显示。当我右键单击并选择复制图像 URL 时,它给了我"http://techyoucation.com/%3Cimg%20alt=''%20src='http://0.gravatar.com/avatar/?d=http://techyoucation.com/wp-content/themes/blank/images/treehouse.png&s=50'%20class='avatar%20avatar-50%20photo%20avatar-default'%20height='50'%20width='50'%20/%3E"

任何想法。

谢谢

你只需使用由wordpress可插拔函数提供的函数,即可获得头像:

代码已更新

<div class="gravatar">
<?php
$user_id = get_current_user_id(); // Get the user ID
$size_avatar = 50; // Set the avatar size
$default_avatar = 'http://techyoucation.com/wp-content/themes/blank/images/treehouse.png'; // Set your default image url
echo get_avatar( $user_id, $size_avatar, $default_avatar ); // This prints the <img> tag with correct avatar image url
?>
</div>