WordPress注册会员用户展示列表(作者墙或用户墙)

WordPress教程 3994

使用WordPRess的用户希望在网站前台展示网站的注册用户或者网站运营团队成员包括头像、昵称、主页链接等信息,通过下面的代码可以实现调用作者会员个人资料里的各种信息,包括自定义增加的个人资料如新浪微博、腾讯微博等。代码默认调用网站所有用户,如果只想调用特定角色可以通过博客吧前面介绍的《WordPress如何判断注册用户角色权限等级》查看各角色的level进行SQL判断筛选调用。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
global $wpdb;
所有作者
$authors = $wpdb->get_results("SELECT ID, user_login from $wpdb->users ORDER BY display_name");
//除了admin外的作者
//$authors = $wpdb->get_results("SELECT ID, user_login from $wpdb->users WHERE user_login <> 'admin' ORDER BY display_name");
//管理员和编辑
$authors = $wpdb->get_results("SELECT ID, user_login from $wpdb->users , $wpdb->usermeta WHERE meta_key = 'wp_user_level' AND meta_value >= 7  AND ID = user_id ORDER BY display_name");
foreach ($authors as $author) {
	//修改过固定链接
	echo "<div>";
	//默认链接
	//echo "<div><a href='".get_bloginfo('url')."?author=".$author->ID."'>".get_avatar($author->ID,50)."</a>";
	//新浪微博
	if(get_the_author_meta('weibo_sina', $author->ID)){
		echo "<a href='".get_the_author_meta('weibo_sina', $author->ID)."'>".get_avatar($author->ID,50)."</a>";
	}
	//腾讯微博
	elseif(get_the_author_meta('weibo_tx', $author->ID)){
		echo "<a href='".get_the_author_meta('weibo_tx', $author->ID)."'>".get_avatar($author->ID,50)."</a>";
	}
	//个人网站
	elseif(get_the_author_meta('url', $author->ID)){
		echo "<a href='".get_the_author_meta('url', $author->ID)."'>".get_avatar($author->ID,50)."</a>";
	}else {
		echo "<a href='".get_bloginfo('url')."/author/".get_the_author_meta('user_login', $author->ID)."'>".get_avatar($author->ID,50)."</a>";
	}
 
	//个人说明
	if(get_the_author_meta('description', $author->ID)){
		echo get_the_author_meta('description', $author->ID);
	}
	echo "</div>";
}
?>

以上代码列出了网站所有用户、除了帐号为admin外的所有用户,只有管理员和编辑三种角色调用,默认调用网站所有会员。

默认使用新浪微博的链接,如果没有新浪微博则使用腾讯微博……(前提是对个人资料进行了扩展,添加有新浪微博、腾讯微博等)

精品推荐: