WordPress Creating User Lists with Pagination Using paginate_links

Development of WordPress themes, many friends like to make a list of users to increase the activity of the site users, creating a list of users is very simple, directly use theget_usersfunction can be realized, but when the number of users is very large, all users in a page display will cause the page to open very slowly, this time for the user list to add a paging function is very necessary, many friends in the realization of the paging function encountered problems. Today we continue to create a user list paging example to introduce the WordPress paginate_links function.

First we need to query and display the list of users, and by the way, prepare the data needed for paging

$number = 10; //number displayed per page
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; //current page
$offset = ($paged - 1) * $number; //offset
$users = get_users(); //get all users
$query = get_users('&offset='.$offset.'&number='.$number); //Users queried on current page
$total_users = count($users); //total number of users
$total_query = count($query); //Total number of queries
$total_pages = intval($total_users / $number) + 1; //total pages

foreach($query as $q) {
    // List the users
}

The principle of paging does not need to say more, the data needed is nothing more than a page to display the amount of data, the total amount of data and the current number of pages, these data we have prepared above, we just need to pass these parameters to the paginate_links Of course, if you have to write raw SQL statements to query, using wpdb directly is also possible.

if ($total_users > $total_query) {
    echo '<div id="pagination" class="clearfix">'; echo '
    echo '<span class="pages">Pages.</span>';

    // Pick the largest of the first page and the query parameter page as the current page number
    $current_page = max(1, get_query_var('paged'));

    //Display pagination
    echo paginate_links(array(
        'base' =&gt; get_pagenum_link(1) . '%_%'.
        'format' =&gt; 'page/%#%/',
        'current' =&gt; $current_page,
        
        'prev_next' =&gt; false, 'prev_next' =&gt; false, 'type' =&gt; 'list'
        'type' =&gt; 'list', .
    ));
    echo '</div>';
}

Using the same approach, we can also implement WordPress Category Tag List PaginationEven in WordPress, you can customize the paging function of the data table by using the paginate_links function. Or if you are more familiar with their own or other paging library, it is the same principle and method.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *