魔改主题时用……

Gravatar是全球通用的头像服务,它是根据用户邮箱来调用他们的头像的。在WordPress主题和插件开发中,很多地方都需要调用到当前用户的Gravatar头像,下面就是简单的实现方法。

将下面的代码添加到主题的 functions.php 文件或者特定的插件文件(如果你开发插件的话)中:

function Get_Gravatar() { 
	global $current_user;
	get_currentuserinfo();
	// 获取用户的邮箱地址
	$getuseremail = $current_user->user_email;
	// 将邮箱转换成MD5哈希值,并设置图像的大小为64像素
	$usergravatar = 'http://www.gravatar.com/avatar/' . md5($getuseremail) . '?s=64';
	echo '<img src="' . $usergravatar . '" class="wpd_gravatar" />';
}

 

然后通过下面的代码调用当前用户的Gravatar头像:

<?php Get_Gravatar(); ?>

 

如果我们需要获取当前登录的用户的其他信息,可以参考下面的代码:

<?php global $current_user;
 get_currentuserinfo();
 echo '用户名: ' . $current_user->user_login . "\n";
 echo '用户邮箱: ' . $current_user->user_email . "\n";
 echo '名字: ' . $current_user->user_firstname . "\n";
 echo '姓氏: ' . $current_user->user_lastname . "\n";
 echo '公开显示名: ' . $current_user->display_name . "\n";
 echo '用户 ID:' . $current_user->ID . "\n";
?>

 

如果需要调用文章作者的相关信息:

<?php the_author(); ?> 显示WordPress文章作者名字代码
<?php the_author_description(); ?> 显示文章作者的描述(作者个人资料中的描述)
<?php the_author_login(); ?> 显示文章作者的登录名
<?php the_author_firstname(); ?> 显示文章作者的firstname(名)
<?php the_author_lastname(); ?> 显示文章作者的lastname(姓)
<?php the_author_nickname(); ?> 显示文章作者的昵称
<?php the_author_ID(); ?> 显示文章作者的ID号
<?php the_author_email(); ?> 显示文章作者的电子邮箱
<?php the_author_url(); ?> 显示文章作者的网站地址
<?php the_author_link (); ?>(添加于2.1版本) 显示一个以文章作者名为链接名,链接地址为文章作者的网址的链接。
<?php the_author_icq(); ?> (不推荐使用) 显示文章作者的icq(稍微diy以下html模板内容即可显示为QQ)
<?php the_author_aim(); ?> 显示文章作者的aim(同上,可以Diy成微信什么的.....反正随意就好)
<?php the_author_yim(); ?> 显示文章作者的yim
<?php the_author_msn(); ?> (不推荐使用) 显示文章作者的msn
<?php the_author_posts(); ?> 显示文章作者已发表文章的篇数
<?php the_author_posts_link(); ?> 显示一个链接到文章作者已发表文章列表的链接.


<?php list_authors(); ?> (不推荐使用) 显示blog所有作者和他们的相关信息。完整函数参数如下:
optioncount:是否显示各作者已发表文章的篇数,可选值为:TRUE 和 FALSE(默认值)
exclude_admin:是否不列出管理员,可选值为:TRUE(默认值) 和 FALSE
show_fullname :是否显示各作者的全名,可选值为:TRUE 和 FALSE(默认值)
hide_empty:是否不显示发表文章数为0的作者,可选值为:TRUE(默认值) 和 FALSE
feed:链接到各个作者发表文章的RSS供稿种子链接名,默认为空,不显示RSS供稿种子
feed_image:供稿种子的图片地址,如果提供此项,则覆盖上面的feed,默认为空


<?php wp_list_authors(); ?> 显示blog作者列表,如果作者发表过文章,则他的名字将链接到他发表的文章列表中。可定义是否显示其他信息,参数如下:
optioncount:是否显示各个作者发表文章数,可选值:true 和 false(默认值)
exclude_admin:是否不显示”admin”用户,可选值:true(默认值) 和 false
show_fullname:是否显示各个作者的全名,如果不显示,将显示昵称。可选值:true 和 false(默认值)
hide_empty:是否不显示发表文章数为0的作者,可选值:true(默认值) 和 false
feed:链接到各个作者发表文章的RSS供稿种子链接名,默认为空,不显示RSS供稿种子
feed_image:供稿种子的图片地址,如果提供此项,则覆盖上面的feed,默认为空。

 

使用the_author_meta也有同样的效果:

the_author_meta( $field, $userID )

 

具体参见wordpress文档

展开全部

Usage

 <?php the_author_meta( $field, $userID ); ?> 

 

Parameters

$field
(string) Field name for the data item to be displayed. Valid values:

  • user_login
  • user_pass
  • user_nicename
  • user_email
  • user_url
  • user_registered
  • user_activation_key
  • user_status
  • display_name
  • nickname
  • first_name
  • last_name
  • description
  • jabber
  • aim
  • yim
  • user_level
  • user_firstname
  • user_lastname
  • user_description
  • rich_editing
  • comment_shortcuts
  • admin_color
  • plugins_per_page
  • plugins_last_view
  • ID
$userID
(integer) (optional) If the user ID fields is used, then this function display the specific field for this user ID.

Default: false

Examples

Display the Author’s AIM screenname

Displays the value in the author’s AIM (AOL Instant Messenger screenname) field.

<p>This author's AIM address is <?php the_author_meta('aim'); ?></p>

 

Display a User Email Address

Displays the email address for user ID 25.

<p>The email address for user id 25 is <?php the_author_meta('user_email',25); ?></p>

 

Advanced Uses

A plugin may add an additional field in the registration or manage users, which adds a new value in the wp_usermeta table (where wp_ is your data base prefix). For this example we will use a Twitter ID. For a meta_key value of “twitter” and meta_value of “WordPress” then

<p>This author's Twitter name is <?php the_author_meta('twitter'); ?></p>

 

would return

This author’s Twitter name is WordPress

注意:以上的函数调用后会直接显示相关信息,而不是返回一个值

如果想要返回一个值供其他函数调用,可使用get_the_author_meta,用法与the_author_meta相同:

get_the_author_meta( $field, $userID )

 

用法同样参考wordpress文档

展开全部

Description

Valid values for the $field parameter include:

  • admin_color
  • aim
  • comment_shortcuts
  • description
  • display_name
  • first_name
  • ID
  • jabber
  • last_name
  • nickname
  • plugins_last_view
  • plugins_per_page
  • rich_editing
  • syntax_highlighting
  • user_activation_key
  • user_description
  • user_email
  • user_firstname
  • user_lastname
  • user_level
  • user_login
  • user_nicename
  • user_pass
  • user_registered
  • user_status
  • user_url
  • yim

Parameters

$field

(string) (Optional) The user field to retrieve.

Default value: ”

$user_id

(int) (Optional) User ID.

Default value: false

Return

(string) The author’s field from the current author’s DB object, otherwise an empty string.

就酱紫……

参考:wordpress大学 X2 xiaohost wordpress文档


本文被阅读了:5,420次

作者头像
关于  Ailitonia

正因站在了巨人的肩膀上,才越发觉得自己渺小。不求成为巨人,但求与其同行。 把自己所见所闻,记录下来。

→查看所有由Ailitonia发布的文章