简体   繁体   English

如何从WordPress的wp_users表中获取所有user_nicename?

[英]how to get all user_nicename from wp_users table in wordpress?

I have this function here: 我在这里有此功能:

function displaymeta(){
        global $post;
        global $wpdb;
        $authors = $wpdb->get_results("select id,user_nicename from $wpdb->users");
        foreach($authors as $auth){
                $authorname = get_the_author_meta('user_nicename',$auth->ID);
                echo $authorname;
        }
}
add_filter( 'the_content', 'displaymeta' );

It should retrieve all the user_nicename from wp_users table in database. 它应该从数据库的wp_users表中检索所有user_nicename。 But when I execute this code, I just get the first username twice, even though there are two distinct users. 但是,当我执行此代码时,即使有两个不同的用户,我也只会两次获得第一个用户名。 What am I missing here? 我在这里想念什么? Thanks! 谢谢!

UPDATE I need to do something like this: 更新我需要做这样的事情:

 $arr=array();
 global $wpdb;
 $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users 
                                ORDER BY display_name");
 foreach($authors as $auth)
 {
    $arr[]=get_author_meta('user_nicename', $auth->ID);
 }

I want to store the user_nicename in an array of all users in the wp_users table. 我想将user_nicename存储在wp_users表中所有用户的数组中。

You can do it very simple like this: 您可以像这样非常简单地进行操作:

$users = get_users();
foreach($users as $user) {
    echo $user->user_nicename;
}

More info here -> https://codex.wordpress.org/Function_Reference/get_users 更多信息在这里-> https://codex.wordpress.org/Function_Reference/get_users

If you simply want an array with just the user_nicenames you can do it like this: 如果只需要一个仅包含user_nicenames的数组,则可以这样操作:

global $wpdb

$user_nicenames = $wpdb->get_results("SELECT user_nicename FROM {$wpdb->prefix}users", ARRAY_A);

foreach($user_nicenames as $nice_name) {
    echo $nice_name;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户已经存在于 wordpress db 的 wp_users 表中? - If user already exists in wp_users table of wordpress db? 如何在登录wordpress的用户上显示wp_users和wp_usermeta的所有信息 - how can I display all info on wp_users and wp_usermeta on user logged in wordpress WordPress在wp_users中插入新用户 - WordPress insert new user in wp_users WordPress:将wp_users和所有关联的元数据从wp_metadata中选择到每个用户记录的一行中 - WordPress: SELECT wp_users and all associated meta data from wp_metadata into one row per user record wordpress从管理员编辑wp_users表字段 - wordpress edit wp_users table field from admin Wordpress:如果数据库中存在,则将 Interger 添加到 User_nicename - Wordpress: Add Interger to User_nicename if it Exists in Database 如何使用表 wp_users 作为登录名? - How to use table wp_users as login? 在共享相同 wp_users 和 wp_usermeta 表的两个 Wordpress 安装之间同步所有用户角色。 - Sync all User Roles between two Wordpress Installs sharing the same wp_users and wp_usermeta tables. 匹配wp_usermeta中的多个值以从wp_users表中获取电子邮件 - Matching multiple values in wp_usermeta to get email from wp_users table 我可以在wordpress中使用用户名代替用户ID,而忽略wp_users表吗? - Can i use username instead of user id in wordpress, ignoring wp_users table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM