Main Menu

How to Display List of Authors with Pagination [WordPress Tip]

In brief, the template tag is a simple way of querying information from the database and displaying it at the front side of your website. For example, we can display the name of the post author by using the_author(); and using the_title(); to display the post or page title.

WordPress template tags are quite extensive, allowing us to fully customize our website without needing third party plugins. In this tutorial, we are going to show you how to create a custom page that display a list of users using a set of these template tags.

In addition, we will also apply pagination to display those users in a number of pages. Such a page might be required for certain websites.

Creating Custom Page Template

First, create a new PHP file in your activated theme directory. In this example, I’m using a TwentyTwelve theme. Add the following comment tag to register it as a page template.

<?php
/*
Template Name: User Page
*/

In the WordPress page editor, set it as the template for your author page.

Note that you shouldn’t name the file as author.php as it has been reserved by WordPress to display the author post archive.

The Query and Formula

Let’s set the formula. Open the new template we have just created in a code editor.

First, we need to specify the number of users we want to show per page. We set this number in a variable named $number. Anytime you want to change the amount of users displayed on the page, change the value in this variable.

$number = 10;

Add the following code snippet to pinpoint the current pagination number.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

Then, using the following code, we count the number of users that should be passed over in the pages (offset) – this will take effect at the second page onwards.

$offset = ($paged - 1) * $number;

We need to get the registered users in our website, and count the total number using the PHP count() function, as follows.

$users = get_users();
$total_users = count($users);

We also count the total number of queried user per our specifications, which include the number of users that will be displayed, and the offset number.

$query = get_users('&offset='.$offset.'&number='.$number);

Then, we count the total pages that should be created. We can count it based on the total registered users and the number of users displayed per page, as follows.

$total_pages = intval($total_users / $number) + 1;

Displaying the Users

As we have set the required formula, now we will display the results on the page.

In this example, we will display a few things from the user: the avatar, the full name, and the short biography (description). These information can be retrieved respectively using the following template tags: get_avatar, and get_the_author_meta

Creating Pagination

Certainly we don’t want to display hundreds of users in a single page. So, we are going to create the pagination link and split the results in multiple pages. In addition, we will only display the pagination if the total registered user number is greater than the users being displayed per page.

Fortunately, WordPress has a template tag that allows us to create pagination easily, called paginate_links().