Display A List Of Authors On WordPress

On some blogs it is important to post on a regular basis, but this can be difficult unless blogging is your full time job.

For a single person to regularly post awesome blog posts everyday can take up a lot of time. For this reason many blogs have a number of authors, this helps keep the content regular and high quality to ensure a better blog.

On a single author blog you will normally find an about page that will tell you information about the author. You can do this on a multi author blog but you also need to create a page that will display all the authors on the blog so you can link to each of their posts.

If you want to create a page to display all the authors you have two options, you can create a new page template which will allow you to code the layout of the page to show all the authors. The other option is to create a shortcode where the user of the site can use this on a new page content to create a list of all the authors.

The two options should be decided on if you are going to add this functionality in a plugin or a theme. If you are going to add this functionality to the theme then you should use a page template, if you want to use the shortcode then you should do this with a plugin. In this tutorial we are going to create a page template to display all the authors on the site.

Create A Page Template

First start off by creating a new page template for your list of all authors.

/*
Template Name: Display All Authors
*/


The next thing we need to do is get a list of all users on the WordPress site that could write a post. This is all the users apart from the users with the subscriber role, the best way of getting a list of all users is to use the function get_users().

This will return an array of all users on your site, it takes a number of different parameters such as:

  • blog_id - The current blog's ID, unless multisite is enabled and another ID is provided
  • role - Limit the returned authors to the role specified.
  • include - An array of IDs. Only users matching these IDs will be returned.
  • exclude - An array of IDs. Users matching these IDs will not be returned, regardless of the other arguments.
  • meta_key - The meta_key in the wp_usermeta table for the meta_value to be returned.
  • meta_value - The value of the meta key.
  • meta_compare - Operator to test the 'meta_value'. Possible values are '!=', '>', '>=', '<', or '<='. Default value is '='.
  • meta_query - A WP_Query style multiple meta_key/meta_value array.
  • orderby - Sort by 'ID', 'login', 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'.
  • order - ASC (ascending) or DESC (descending).
  • offset - The first n users to be skipped in the returned array.
  • search - Use this argument to search users by email address, URL, ID or username (this does not currently include display name).
  • number - Limit the total number of users returned.
  • count_total - False by default.
  • fields - Which fields to include in the returned array. Default is "all". If set to 'all_with_meta', returns an array of WP_User objects keyed by ID.
  • who - If set to 'authors', only authors (user level greater than 0) will be returned.

The parameters that we are going to be using is the orderby and order parameters. This is because we are going to use the orderby parameter to display the authors in order of the amount of posts they have wrote. Then we are going to use the order parameter to order the users is DESC order to display the the user with the most posts first.

The other parameter that we could use is the role parameter to bring a back users with a specific role, or we can leave this blank to bring back all users. For this author list we want to display all users except for users with the subscriber role, so we are going to leave this blank and return all users.

$allUsers = get_users('orderby=post_count&order=DESC');

As we have all the users we need to filter these and remove the users with the subscriber role, this can easily be done by looping through all the users, checking their role and creating a new array for user's which are not subscribers.

$users = array();

// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser)
{
	if(!in_array( 'subscriber', $currentUser->roles ))
	{
		$users[] = $currentUser;
	}
}

Now we have a new $users array which is populated with all users which are not subscribers, we can now loop through this array to display each user on the page.

The user information we are going to display is the user name, user image, a link to all their posts, author description and links to add author meta information.

<?php
foreach($users as $user)
{
	?>
	<div class="author">
		<div class="authorAvatar"></div>
		<div class="authorInfo">
			<h2 class="authorName"></h2>
			<p class="authorDescrption"></ p>
			<p class="authorLinks"></ p>

			<p class="socialIcons"></ p>
		</div>
	</div>
<?php
}
?>

Display Author Avatar

To display the author avatar we can use the author email with the function get_avatar() to display the author avatar.

<div class="authorAvatar">
	<?php echo get_avatar( $user->user_email, '128' ); ?>
</div>

Display Author Name

To display the author name we can use a property value on the user object we are looping through by using $user->display_name;

<h2 class="authorName"><?php echo $user->display_name; ?></h2>

Display Author Description

To display the author description we can use the function get_user_meta() to get the author description.

<p class="authorDescrption"><?php echo get_user_meta($user->ID, 'description', true); ?></p>

Link To All Author Posts

To display a link to all the author posts you can use the function get_author_posts_url().

<p class="authorLinks"><a href="<?php echo get_author_posts_url( $user->ID ); ?>">View Author Links</a></p>

Display Other User Meta Information

You can assign new contact information to your users which you can display on the author list by using the function get_user_meta(). In this tutorial we are using this function to display links to the user different social media profiles.

<p class="socialIcons">
<ul>
	<?php
	$website = $user->user_url;
	if($user->user_url != '')
	{
		printf('<li><a href="%s">%s</a></li>', $user->user_url, 'Website');
	}

	$twitter = get_user_meta($user->ID, 'twitter_profile', true);
	if($twitter != '')
	{
		printf('<li><a href="%s">%s</a></li>', $twitter, 'Twitter');
	}

	$facebook = get_user_meta($user->ID, 'facebook_profile', true);
	if($facebook != '')
	{
		printf('<li><a href="%s">%s</a></li>', $facebook, 'Facebook');
	}

	$google = get_user_meta($user->ID, 'google_profile', true);
	if($google != '')
	{
		printf('<li><a href="%s">%s</a></li>', $google, 'Google');
	}

	$linkedin = get_user_meta($user->ID, 'linkedin_profile', true);
	if($linkedin != '')
	{
		printf('<li><a href="%s">%s</a></li>', $linkedin, 'LinkedIn');
	}
?>
</ul>
</p>

By putting the above code inside the loop you can display all the information you need to show your visitors all about the authors of your blog.

Full Page Template

Here is the full page template you can use on theme.

<?php
/*
Template Name: Display Authors
*/

// Get all users order by amount of posts
$allUsers = get_users('orderby=post_count&order=DESC');

$users = array();

// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser)
{
	if(!in_array( 'subscriber', $currentUser->roles ))
	{
		$users[] = $currentUser;
	}
}

?>

<?php get_header(); ?>

<section class="content" role="main">
	<?php
		printf('<h1>%s</h1>', the_title());

		foreach($users as $user)
		{
			?>
			<div class="author">
				<div class="authorAvatar">
					<?php echo get_avatar( $user->user_email, '128' ); ?>
				</div>
				<div class="authorInfo">
					<h2 class="authorName"><?php echo $user->display_name; ?></h2>
					<p class="authorDescrption"><?php echo get_user_meta($user->ID, 'description', true); ?></p>
					<p class="authorLinks"><a href="<?php echo get_author_posts_url( $user->ID ); ?>">View Author Links</a></p>

					<p class="socialIcons">
						<ul>
							<?php
								$website = $user->user_url;
								if($user->user_url != '')
								{
									printf('<li><a href="%s">%s</a></li>', $user->user_url, 'Website');
								}

								$twitter = get_user_meta($user->ID, 'twitter_profile', true);
								if($twitter != '')
								{
									printf('<li><a href="%s">%s</a></li>', $twitter, 'Twitter');
								}

								$facebook = get_user_meta($user->ID, 'facebook_profile', true);
								if($facebook != '')
								{
									printf('<li><a href="%s">%s</a></li>', $facebook, 'Facebook');
								}

								$google = get_user_meta($user->ID, 'google_profile', true);
								if($google != '')
								{
									printf('<li><a href="%s">%s</a></li>', $google, 'Google');
								}

								$linkedin = get_user_meta($user->ID, 'linkedin_profile', true);
								if($linkedin != '')
								{
									printf('<li><a href="%s">%s</a></li>', $linkedin, 'LinkedIn');
								}
							?>
						</ul>
					</p>
				</div>
			</div>
			<?php
		}
	?>
</section>

<?php get_footer(); ?>

Advertise here

Comment