且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 WordPress 对象数组中获取数据?

更新时间:2023-11-30 22:42:04

阅读代码

roles[0];}?>

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

I am trying the get the value of the WordPress user. I am trying with the get_users() wp function. How can I get the [ user role ] value form the roles key from this array object. my php code is

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

What return the below code

  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
      }

Read the codex

<?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