designinfantry
Member
Posted 2 years ago #
How do I list all authors of my site in a format as such?
Alex Kwa Profile | Twitter | Email
Rafdi Profile | Twitter | Email
Abel Profile | Twitter | Email
See http://www.designinfantry.com for my site. Am referring to the right sidebar.
<?php
//displays all users ($users will also contain all usermeta fields for each user
$blogusers = get_users_of_blog();
if ($blogusers) {
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->user_id);
// echo "<pre>"; print_r($user); echo "</pre>";
echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . '</p>';
}
}
?>
Take the two slashes off the // echo "<pre>"; print_r($user); echo "</pre>"; statement to see all the values available to you
designinfantry
Member
Posted 2 years ago #
Hello,
thanks for your reply.
But I would like the link of the fullname of the authors to take me to a page where all the posts are that of the authors. Exactly like how the links of wp_list_authors work.
Can that be done?
Didn't test this, but after
$user = get_userdata($bloguser->user_id);
add
$post_count = get_usernumposts($user->ID);
if ($post_count) {
$author_posts_url = get_author_posts_url($key);
echo '<li><a href="' . $author_posts_url . '">' . $user->user_firstname . ' ' . $user->user_lastname . '</a> (' . $post_count . ') </li>';
}
Related:
Stepping Into Template Tags
Stepping Into Templates
Template Hierarchy
designinfantry
Member
Posted 2 years ago #
Thanks
This is my final code in the end.
<?php
//displays all users ($users will also contain all usermeta fields for each user
$blogusers = get_users_of_blog();
if ($blogusers) {
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->user_id);
$post_count = get_usernumposts($user->ID);
if ($post_count) {
echo '<a href="http://www.designinfantry.com/?author=' . $user->ID . '">' . $user->user_firstname . ' ' . $user->user_lastname . '</a><br />';
} else {
echo ' ' . $user->user_firstname . ' ' . $user->user_lastname . '<br />';
}
}
}
?>
This is exactly what I've been looking for. I have 2 questions though:
- How can I exclude admin users or users by id?
- How can I sort the results by user meta field?
In the code below I'm trying to sort the results by the State field in DESC order and also exclude the administrator role from the results.
$blogusers = get_users_of_blog();
if ($blogusers) {
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->user_id);
// Get User Fields
$city = $user->city; $state = $user->state; $phone = $user->phone; $user_email = $user->user_email;
echo '<li>' . $user->user_firstname . ' ' . $user->user_lastname . '</li>';
if($city != null) echo('<li>' . $city . ', ' . $state . '</li>');
if($phone != null) echo('<li>Phone: ' . $phone . '</li>');
if($user_email != null) echo('<li>Email: <a href="mailto:' . $user_email . '?subject=Question from Example.com">' . $user_email . '</a></li>');
}
}