gpt4 book ai didi

php - 使用 get_user() 对姓氏进行排序时遇到问题

转载 作者:可可西里 更新时间:2023-11-01 08:09:53 27 4
gpt4 key购买 nike

我希望我的目录按姓氏排序。我怎样才能做到这一点?

    function dispDirectory()
{
global $user_ID;

// Disallow directory if there is a messaging administrator and this person is not it.
if ($this->admin_user_id && $this->admin_user_id != $user_ID) {
return '';
}

$users = $this->get_users();
$directory = "";


foreach($users as $u)
{
$firstName = get_user_meta($u->ID, 'first_name', true);
$lastName = get_user_meta($u->ID, 'last_name', true);

$directory .= '<p><strong>'.$lastName.', '.$firstName.'</strong> - <a href="'.$this->actionURL.'newmessage&to='.$u->ID.'">'.__('Send Message', 'cartpaujpm').'</a></p>';
}
return $directory;
}

为了排序,我尝试在 foreach 循环之前添加以下内容:

 usort($users, create_function('$a, $b', 'return strnatcasecmp($a->last_name, $b->last_name);'));

这重新组织了列表,但不是按字母顺序(或据我所知是任何有用的顺序)

最佳答案

在调用 get_users() 时,您可以传递允许您按元值排序的参数。参见 this WP Codex .

一个例子(未经测试)可以是:

$users = $this->get_users([
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
]);

我相信你的例子不会工作,因为 User 对象上不存在 last_name 属性(而是 User Meta ).

关于php - 使用 get_user() 对姓氏进行排序时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40955109/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com