Authors List Shortcode
Adds an [authors_list] shortcode that shows everyone who has published posts, with avatar, short bio and number of posts, linking to each author's archive, for "Our writers" pages.
Opens WPIDE on
Code
add_action( 'init', function () {
add_shortcode( 'authors_list', function ( $atts ) {
$atts = shortcode_atts( array( 'orderby' => 'post_count', 'bio' => 'yes', 'post_type' => 'post' ), $atts, 'authors_list' );
$users = get_users( array(
'has_published_posts' => array( sanitize_key( $atts['post_type'] ) ),
'orderby' => in_array( $atts['orderby'], array( 'post_count', 'display_name', 'registered' ), true ) ? $atts['orderby'] : 'post_count',
'order' => 'post_count' === $atts['orderby'] ? 'DESC' : 'ASC',
'number' => 50,
) );
if ( ! $users ) {
return '';
}
$counts = count_many_users_posts( wp_list_pluck( $users, 'ID' ), sanitize_key( $atts['post_type'] ), true );
$html = '<ul class="wpide-authors-list" style="list-style:none;padding:0;display:grid;gap:1.25rem">';
foreach ( $users as $user ) {
$bio = 'yes' === $atts['bio'] ? get_the_author_meta( 'description', $user->ID ) : '';
$html .= sprintf(
'<li style="display:flex;gap:1rem;align-items:flex-start">%s<div><a href="%s"><strong>%s</strong></a> <small>(%s)</small>%s</div></li>',
get_avatar( $user->ID, 64, '', '', array( 'extra_attr' => 'style="border-radius:50%;flex:none" loading="lazy"' ) ),
esc_url( get_author_posts_url( $user->ID ) ),
esc_html( $user->display_name ),
esc_html( sprintf( _n( '%s post', '%s posts', (int) $counts[ $user->ID ] ), number_format_i18n( (int) $counts[ $user->ID ] ) ) ),
$bio ? '<p style="margin:.3em 0 0">' . esc_html( wp_trim_words( $bio, 30 ) ) . '</p>' : ''
);
}
return $html . '</ul>';
} );
} );
Notes
Example [authors_list orderby="post_count" bio="yes"]; users with no published posts are left out.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.