1

我正在尝试在页面上显示所有用户的列表。它需要显示他们的姓名、电子邮件地址、网站 URL 和几个自定义作者元字段。

我从以下查询开始:

<?php
//displays all users with their avatar and their posts (titles)
$blogusers = get_users_of_blog();
if ($blogusers) {
  foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->user_id);
    echo get_avatar( $user->ID, 46 );
    echo '<div><p>User ID ' . $user->ID . ' ' 
                            . $user->user_firstname . ' ' 
                            . $user->user_lastname . ' ' 
                            . $user->user_url . ' ' 
                            . $user->user_description 
             . '</p></div>';
    $args=array(
      'author' => $user->ID,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );

  }
}
?>

这显示了基本的用户信息,但没有自定义作者元字段。我将其调整为:

<?php
//displays all users with their avatar and their posts (titles)
$blogusers = get_users_of_blog();
if ($blogusers) {
  foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->user_id);
    echo get_avatar( $user->ID, 46 );
    echo '<div><p>User ID ' . $user->ID . ' ' 
                            . $user->user_firstname . ' ' 
                            . $user->user_lastname . ' ' 
                            . $user->user_url . ' ' 
                            . $user->user_description 
           . '</p>'; ?>
        <?php the_author_meta('position', $user);?>
        <?php the_author_meta('telephone');?>
        <?php the_author_meta('linkedin');?>
        <?php the_author_meta('vcard');?>
    <?php echo '</div>';?><?php 
    $args=array(
      'author' => $user->ID,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );

  }
}
?>

这适用于第一个用户,但对于第二个用户,它会显示第一个用户的自定义作者元数据。

我花了一个小时试图弄清楚如何进一步修改它以显示每个用户自己的自定义作者元数据,但是我无法让它工作。

如何修改?

此外,我需要能够排除某些用户 ID - 我尝试添加“排除”并输入我想要排除的 ID,但它不起作用。

请帮忙

4

3 回答 3

1

要使用函数获取特定用户的元数据,the_author_meta()您需要传递用户 ID,即

php the_author_meta('telephone' 25); // will get the telephone field's value of user with id 25

所以在你的代码中你可以使用

<?php the_author_meta('position', $user->ID);?><?php the_author_meta('telephone', $user->ID);?><?php the_author_meta('linkedin', $user->ID);?><?php the_author_meta('vcard', $user->ID);?>

要排除某些用户,您可以使用函数检查用户 ID in_array,即

$excluded_users = array(10, 11, 12); // User Ids to exclude
foreach ($blogusers as $bloguser) {
    if (!in_array($bloguser->user_id, $excluded_users))
    {
        $user = get_userdata($bloguser->user_id);
        echo get_avatar( $user->ID, 46 );
        //...
    }
}

参考: 获取用户的博客功能已被弃用,更多关于the_author_meta

于 2012-08-30T19:14:50.717 回答
0

the_author_meta( $key);始终检索有关作者的元信息。如果您想要任何用户的元数据,您需要使用get_user_meta($user_id, $key, $single);$user->ID用作 id。

您可以在Codex中找到更多信息。

于 2012-08-30T19:13:27.300 回答
0

尝试一下

echo get_the_author_meta('address', $user->ID);

见网址

https://wordpress.stackexchange.com/questions/10091/author-custom-fields-post-meta-the-code

于 2012-08-30T19:16:11.237 回答