In this WordPress tutorial, We'll list all authors using the wp_list_authors function. You can use this WordPress functionality in different scenarios. To make this function more useful, we'll create a shortcode for printing all authors.
wp_list_authors function syntax
Listing authors in WordPress is possible by using the wp_list_authors
function.
There was a list_authors
function available in WordPress too, but it is deprecated from the WordPress version 2.1.0!
WordPress recommends that you use
wp_list_authors
instead oflist_authors
.
wp_list_authors($args = "")
the $args
parameter can be an array or string. You can specify arguments to customize the result based on your needs.
What can be inside the $args array for wp_list_authors function?
- orderby: determines what is the order based on. possible values are: ‘nicename’, ‘ID’, ’email’, ‘url’, ‘registered’, ‘user_nicename’, ‘user_email’, ‘user_url’, ‘user_registered’, ‘name’, ‘display_name’, ‘post_count’, ‘meta_value’, ‘user_login’.
- order: sets order direction. it can be one these two values: ASC or DESC.
- number: sets limit for authours you want to get or print. value must be an integer. if you do not set this, the function will use all authors.
- optioncount: displays how many posts this author has. value can be true or false (default: false).
- exclude_admin: exclude admin users from the list. value can be true or false (default: true).
- show_fullname: determines if fullname must be displayed or not. value can be true or false (default: false).
- hide_empty: it tells WP to exclude authors with no posts. value can be true or false (default: true).
- feed: displays a text link to authors feed. value must be string.
- feed_image: displays an image link to authors feed. value must be string.
- feed_type: sets the type of feed we want to display. value can be ‘rss2’, ‘atom’.
- echo: requests the print action for this function. value can be true or false. if it sets to true, you do not need to use php echo function (default: true).
- style: If value sets to
list
, authors will be inside a<li>
tag, other values will generate comma separated authors information. - html: determines if you want the output be in HTML or plain text (default: true, HTML format).
- exclude: excludes authors by IDs. it accepts comma separated user IDs or an array containing IDs (
int[]
). - include: includes authors by IDs. it accepts comma separated user IDs or an array containing IDs (
int[]
).
wp_list_authors function example
In this example, we’re going to print a list of authors using the wp_list_authors
function in WordPress.
I set a number of 10 authors (admins included) to be printed with the number of their posts displayed ordered by user ID.
In this example, printing style is list and authors with no posts are excluded.
$args = array(
'orderby' => 'ID',
'order' => 'ASC',
'number' => 10,
'optioncount' => true,
'exclude_admin' => false,
'show_fullname' => false,
'hide_empty' => true,
'echo' => true,
'style' => 'list',
'html' => true,
'exclude' => '',
'include' => '',
);
wp_list_authors($args);
Look at the echo
field in $args
array. It defines that we need the authors’ list to be printed by the function itself.
If the echo
field was set to false, you must use the PHP echo command to print the list.
Listing authors with a shortcode in WordPress
In this example, We’ll create a shortcode to print a list of authors when we want.
You can use this code inside the functions.php
file of your current theme Or create a new WordPress plugin for yourself.
function print_my_authors_in_list($atts){
$args = array(
'orderby' => isset($atts['orderby']) ? $atts['orderby'] : 'name',
'order' => isset($atts['order']) ? $atts['order'] : 'ASC',
'number' => isset($atts['number']) ? $atts['number'] : '',
'optioncount' => isset($atts['optioncount']) ? $atts['optioncount'] : false,
'exclude_admin' => isset($atts['exclude-admin']) ? $atts['exclude-admin'] : true,
'show_fullname' => isset($atts['show-fullname']) ? $atts['show-fullname'] : false,
'hide_empty' => isset($atts['hide-empty']) ? $atts['hide-empty'] : true,
'echo' => false,
'style' => isset($atts['style']) ? $atts['style'] : 'list',
'html' => isset($atts['html']) ? $atts['html'] : true,
'exclude' => isset($atts['exclude']) ? $atts['exclude'] : '',
'include' => isset($atts['include']) ? $atts['include'] : '',
);
return wp_list_authors($args);
}
add_shortcode("print-my-authors-list","print_my_authors_in_list");
// try to execute the shortcode:
// use 0 or 1 instead of true/false
echo do_shortcode('[print-my-authors-list exclude-admin="0" optioncount="1"]');
In the above example, We executed the shortcode with the do_shortcode
function.
You also can use this shortcode inside WordPress editor.
written by Mehdi Nazari about in WordPress WordPress Functions WordPress Plugin Development WordPress Theme Development
What do you think about "How to List All Authors in WordPress (Shortcode Included!)"?