之前我们分享过 WordPress 显示最近注册的用户,今天再来实现 随机显示已注册的用户。方法其实都几乎一样,仅仅是查询数据库的代码有点差异罢了。
将下面的代码添加到当前主题的 functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * 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 rel="nofollow noopener noreferrer" href="'.$username->user_url.'">'.$username->user_nicename."</a></li>"; endif; } $randomusers .= '</ul>'; return $randomusers; } // 添加简码 add_shortcode('randomusers','wpb_random_users'); |
/** * 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 rel="nofollow noopener noreferrer" href="'.$username->user_url.'">'.$username->user_nicename."</a></li>"; endif; } $randomusers .= '</ul>'; return $randomusers; } // 添加简码 add_shortcode('randomusers','wpb_random_users');
然后你就可以在模板中使用下面 的函数调用
1 | <?php wpb_random_users(); ?> |
<?php wpb_random_users(); ?>
或者在文章、页面或小工具中使用下面的简码调用
[randomusers]
参考资料:http://www.wpbeginner.com/wp-tutorials/how-to-randomly-display-registered-users-in-wordpress/
到此这篇关于WordPress 随机显示已注册的用户就介绍到这了。无论你有多少朋友,不能少了书这个诤友。更多相关WordPress 随机显示已注册的用户内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!