If you are making a custom page in WordPress where you want to show the user listing with numbered pagination then you are in right place.
In this tutorial, I will share code using which you can make a Custom user listing with numbered pagination
There are two methods to make this functionality.
- Using custom WordPress template
- Using shortcode
As mentioned above, there are two different methods using which you can make a custom user listing.
Using custom WordPress template
Custom WordPress templates are the files that are used by WordPress to have some templating which we can use for our custom codings and designs. For this method, follow the below steps:
Step 1 – Make A WordPress template
Make a new file inside your active WordPress theme with any name. Let say we made the file with the name “userListing.php” inside the theme. Now add the following code inside the newly created file
1 2 3 4 | <?php /* Template Name: User listing(TPL) */ |
Step 2 – Make a page in WordPress admin
Now create a page in the WordPress admin and select the template name from “page attributes” from the right panel.
Step 3 – Adding User listing code
Add the following code to “userListing.php
” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php $args = array( 'role' => 'um_member', 'orderby' => 'login', 'order' => 'ASC', ); $number = 48; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $offset = ($paged - 1) * $number; $users = get_users($args); $query = get_users('&offset='.$offset.'&number='.$number.'&role=um_member'); $total_users = count($users); $total_query = count($query); $total_pages = intval($total_users / $number) + 1; ?> <div class="container"> <div class="row"> <?php foreach($query as $q) { ?> <div class="col-lg-1 col-sm-6 mb-1 minheight"> <div class="card"> <div class="card-body"> <?php echo get_avatar( $q->ID, 80 ); ?> <h5 class="card-title"><?php echo get_the_author_meta('display_name', $q->ID)?get_the_author_meta('display_name', $q->ID):'No name' ; ?></h5> </div> </div> </div> <?php } ?> </div> </div> <?php if ($total_users > $total_query) { echo '<div id="dcsdemopagination" class="clearfix">'; $current_page = max(1, get_query_var('paged')); echo paginate_links(array( 'base' => get_pagenum_link(1) . '%_%', 'format' => 'page/%#%/', 'current' => $current_page, 'total' => $total_pages, 'prev_next' => false, 'type' => 'list', )); echo '</div>'; } |
Using shortcode
Shortcodes are another very useful functionality provided by WordPress. Shortcode is basically made to use a specific syntax that has a callback defined to execute a set of codes.
With this method, you don’t need to make a custom PHP file inside your theme. Start from step 2 i.e add a new WordPress page from admin and paste the following code inside the description section.
1 | [dcsUserslistWithPagination] |
To make this shortcode, paste the following code in functions.php
of your active WordPress theme.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?php add_shortcode('dcsUserslistWithPagination','dcsUserslistWithPagination'); function dcsUserslistWithPagination(){ $args = array( 'role' => 'um_member', 'orderby' => 'login', 'order' => 'ASC', ); $number = 48; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $offset = ($paged - 1) * $number; $users = get_users($args); $query = get_users('&offset='.$offset.'&number='.$number.'&role=um_member'); $total_users = count($users); $total_query = count($query); $total_pages = intval($total_users / $number) + 1; ?> <div class="container"> <div class="row"> <?php foreach($query as $q) { ?> <div class="col-lg-1 col-sm-6 mb-1 minheight"> <div class="card"> <div class="card-body"> <?php echo get_avatar( $q->ID, 80 ); ?> <h5 class="card-title"><?php echo get_the_author_meta('display_name', $q->ID)?get_the_author_meta('display_name', $q->ID):'No name' ; ?></h5> </div> </div> </div> <?php } ?> </div> </div> <?php if ($total_users > $total_query) { echo '<div id="dcsdemopagination" class="clearfix">'; $current_page = max(1, get_query_var('paged')); echo paginate_links(array( 'base' => get_pagenum_link(1) . '%_%', 'format' => 'page/%#%/', 'current' => $current_page, 'total' => $total_pages, 'prev_next' => false, 'type' => 'list', )); echo '</div>'; } } |
All set. Now your page is ready o show Custom user listing with numbered pagination
Want to check the live demo on our website?
If you have any queries, please use our questions bank section