[英]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.