For multi-user WordPress sites, you may want to showcase your users on different sections of your website. For example, you can display a 带有头像的作者列表, or add an 作者信息框, etc. In this tutorial we will show you how to display recently registered users in WordPress. These users do not need to be 作者. It can be used for a community site that allows for user registration.
对于多用户WordPress网站,您可能希望在网站的不同部分展示用户。 例如,您可以显示带有头像的作者列表 ,或添加作者信息框等。在本教程中,我们将向您展示如何显示WordPress中最近注册的用户。 这些用户不需要是作者 。 它可以用于允许用户注册的社区站点。
First thing you need to do is copy and paste the following code in your theme’s functions.php
file or in a 特定于站点的插件.
您需要做的第一件事是将以下代码复制并粘贴到主题的functions.php
文件或具体的于在网站的插件中 。
function wpb_recently_registered_users() {
global $wpdb;
$recentusers = '<ul class="recently-user">';
$usernames = $wpdb->get_results("SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY ID DESC LIMIT 5");
foreach ($usernames as $username) {
if (!$username->user_url) :
$recentusers .= '<li>' .get_avatar($username->user_email, 45) .$username->user_nicename."</a></li>";
else :
$recentusers .= '<li>' .get_avatar($username->user_email, 45).'<a href="'.$username->user_url.'">'.$username->user_nicename."</a></li>";
endif;
}
$recentusers .= '</ul>';
return $recentusers;
}
现在,您可以通过在主题模板文件中使用以下模板标签(例如 sidebar.php、footer.php 等)来显示网站上的用户:
现在,您可以通过在主题的模板文件中使用以下模板标签(例如 sidebar.php、footer.php 等)在您的网站上显示用户:
<?php wpb_recently_registered_users(); ?>
如果您想在特定页面上显示新注册的用户而不创建页面模板,则可以使用短代码。
如果您想在特定页面上显示新注册的用户而不创建页面模板,可以使用短代码。
只需将此代码添加到主题的functions.php 文件或特定于站点的插件中,就在您之前输入的代码下方。
只需将此代码添加到主题的functions.php 文件或特定于站点的插件中,就在您之前输入的代码下方。
add_shortcode('wpb_newusers', 'wpb_recently_registered_users');
此代码将创建一个新的短代码供您在帖子、页面或小部件中使用。像这样使用它:
此代码将创建一个新的短代码供您在帖子、页面或小部件中使用。像这样使用它:
[wpb_newusers]
[wpb_newusers]
我们希望本文能帮助您在 WordPress 中显示最近注册的用户。如需反馈和问题,请发表评论。
我们希望本文能帮助您在 WordPress 中显示最近注册的用户。如需反馈和问题,请发表评论。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-recently-registered-users-in-wordpress/