How to display all Authors in WordPress:
Developing plugins and themes for WordPress do sometime require the knowledge of WordPress user management. Recently when developing a WordPress theme my client asked for a separate page where all the authors of this site will be displayed along with their profile images and info. I searched WordPress documentation for functions displaying authors and came across this interesting function which I am going to share here for helping you speeding up the development process.
That amazing function which helped me is get_users($args)
According to WordPress documentation get_users() function supports a number of arguments OR Parameters but our concerned arguments are, ‘role’ and ‘ order’.
Code segment demonstrating the use of get_user() function for displaying all authors. If you just want to display authors list, copy and paste this below code in your project and change it according to your requirements. This code segment is in working state along with its style sheet.
<?php
$args2 = array(
‘role’ => ‘administrator’,
‘orderby’ => ‘display_name’,
‘order’ => ‘ASC’
<?php
$args2 = array(
‘role’ => ‘administrator’,
‘orderby’ => ‘display_name’,
‘order’ => ‘ASC’
);
$authors = get_users($args2);
echo ‘<div class=”all-Authors”>’;
foreach ($authors as $author) {
?>
<div class=”single-Author”>
<div class=”author-Image”>
<?php echo get_avatar( $ author, 150 );?>
</div><!– author-Image end–>
<div class=”author-Info”>
<div class=”autor-Name”>
<?php echo $ author ->display_name; ?>
</div><!– author-Name end–>
<div class=”author-Bio”>
<?php echo $ author ->user_description;?>
</div><!– author-Bio end–>
</div><!– author-Info end–>
</div><!– single-Author end–>
<?php
}
?>
</div><!– all-Author end–>
Css stylesheet
.single-Author{ width: 750px; clear: both; border: 1px solid #cccccc; padding: 0px; /* display: inline; */ /* position: relative; */ height: auto; overflow: auto; margin-bottom: 6px; padding:8px; } .author-Image{ width: 156px; height: 156px; float: left; border: 3px solid #97acad; border-radius: 7px; } .author-Info{ width: 500px; height: auto; float: left; margin-left: 15px; margin-top: 8px; }
Step 1:
Passing argument to get_user($args2) function
$args2 = array(
‘role’ => ‘administrator’,
‘orderby’ => ‘display_name’,
‘order’ => ‘ASC’
);
Our first argument ‘role’:
‘role’ supports different values like ‘role’=’administrators’ OR ‘role’=’authors’ OR ‘role’=’subscribers’. But in our case we will use ‘role’=’authors’ as we are only interested only in displaying authors.
Our Second Argument ‘orderby’:
This argument is only for sorting the result and supports these values ‘orderby’=’ID’, ‘orderby’=’login’, ‘orderby’=’nicename’, ‘orderby’=’email’, ‘orderby’=’url’, ‘orderby’=’display_name’, ‘orderby’=’registered, ‘orderby’=’post_count’, ‘orderby’=’meta_value’ etc.
Our Third Argument ‘order’:
This argument supports only two values that may be ASC OR DESC. For sorting the authors list in ascending order ‘order’=’ASC’ and for descending ‘order’=’DESC’.
Step 2:
Calling get_users($args2) function will return the list and we will save this list in an array ‘$authors’.
$authors = get_users($arg2);
Step 3:
Using foreach loop for displaying author’s name, bio and their profile images.
foreach ($authors as $author) {
echo get_avatar( $ author, 150 ); // will display author profile image
echo $ author ->display_name; // will display author display name
echo $ author ->user_description; // will display author bio.
}