WordPress 随机显示已注册的用户

之前我们分享过 WordPress 显示最近注册的用户,今天再来实现 随机显示已注册的用户。方法其实都几乎一样,仅仅是查询数据库的代码有点差异罢了。

将下面的代码添加到当前主题的 functions.php:

/**
 * WordPress 随机显示已注册的用户
 * https://www.wpdaxue.com/display-random-registered-users.html
 */
function wpb_random_users() { 
	global $wpdb;
	$randomusers = '<ul class="random-users">';
	// 查询数据库,随机获取 5 位已注册用户
	$usernames = $wpdb->get_results("SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY RAND() LIMIT 5");
	// 将用户已列表的形式显示
	foreach ($usernames as $username) {
		if (!$username->user_url) :
			$randomusers .= '<li>' .get_avatar($username->user_email, 45) .$username->user_nicename."</li>";
		else :
			$randomusers .= '<li>' .get_avatar($username->user_email, 45).'<a href="'.$username->user_url.'">'.$username->user_nicename."</a></li>";
		endif;
	}
	$randomusers .= '</ul>';
	return $randomusers;  
}
// 添加简码
add_shortcode('randomusers','wpb_random_users');

然后你就可以在模板中使用下面 的函数调用

<?php wpb_random_users(); ?>

或者在文章、页面或小工具中使用下面的简码调用

[randomusers]

参考资料:http://www.wpbeginner.com/wp-tutorials/how-to-randomly-display-registered-users-in-wordpress/

来源:

https://www.wpdaxue.com/display-random-registered-users.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33136.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?