【问题标题】:Get data from WordPress object array?从 WordPress 对象数组中获取数据?
【发布时间】:2016-01-13 19:08:59
【问题描述】:

我正在尝试获取 WordPress 用户的价值。我正在尝试使用 get_users() wp 函数。如何从该数组对象中获取来自 roles 键的 [用户角色] 值。 我的php代码是

echo '<pre>';
var_dump(get_users());
echo '</pre>';

下面的代码返回什么

  array(4) {
      [0]=>
      object(WP_User)#510 (7) {
        ["data"]=>
        object(stdClass)#506 (10) {
          ["ID"]=>
          string(2) "12"
          ["user_login"]=>
          string(4) "demo"
          ["user_pass"]=>
          string(34) "$P$Bp7wAWdn9qAPehmbEVcGz7DGGjc.lm1"
          ["user_nicename"]=>
          string(4) "demo"
          ["user_email"]=>
          string(18) "demo@something.com"
          ["user_url"]=>
          string(19) "http://demouser.com"
          ["user_registered"]=>
          string(19) "2015-10-14 12:34:02"
          ["user_activation_key"]=>
          string(45) "1444826043:$P$BT2rYlAjdNo5OqzG7U3CvZw/sdnFqE."
          ["user_status"]=>
          string(1) "0"
          ["display_name"]=>
          string(9) "demo user"
        }
        ["ID"]=>
        int(12)
        ["caps"]=>
        array(1) {
          ["new_contributor"]=>
          bool(true)
        }
        ["cap_key"]=>
        string(15) "wp_capabilities"
        ["roles"]=>
        array(1) {
          [0]=>
          string(15) "new_contributor"
        }
        ["allcaps"]=>
        array(4) {
          ["read"]=>
          bool(true)
          ["edit_posts"]=>
          bool(true)
          ["delete_posts"]=>
          bool(false)
          ["new_contributor"]=>
          bool(true)
        }
        ["filter"]=>
        NULL
      }

【问题讨论】:

标签: wordpress


【解决方案1】:

阅读法典

<?php
$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
    echo $user->roles[0];
}
?>

https://codex.wordpress.org/Function_Reference/get_users

【讨论】:

  • 感谢您的帮助@Nozifel
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 2018-06-28
  • 1970-01-01
  • 2017-10-06
  • 2021-09-14
  • 2014-05-04
  • 1970-01-01
相关资源
最近更新 更多