webappguides
Create Wordpress Theme From Scratch! Build Your Own

Create dynamic pagination in wordpress

webapps log

webapps Lab
June 18, 2021
ThemeCreation

In this post, you will find how to create dynamic pagination that is formed during the search.

A blog with many contents may return many posts for specific search keywords.

Populating all search results on a single displaying page may harm search speed.

This is avoidable if dynamic pagination is added to the search results page and archive.

The pagination numbers can shrink or expand depending on the number of returned results.

Actually, it is simple to include such pagination if you have basic on how to create WordPress Pagination.

You can find more details here.

How to create pagination on home page

Numeric pagination.

For now, let’s implement WordPress dynamic pagination.

Dynamic Pagination Snippet Code.

Add a new function in the functions.php

In this example, the function name is dynamicPagination.

1.function dynamicPagination(){

2.global $wp_query;

3.$allCreatedPages = $wp_query -> max_num_pages;

4.$theBigNumber = 999999999;

5.$dynamicPaginationArgs = array(

6.'base' => str_replace($theBigNumber,'%#%',esc_url (get_pagenum_link($theBigNumber))),

7.'format' => '?page = %#%',

8.'current' =>  max(1, get_query_var ('paged')),

9.'total' => $allCreatedPages

10.);

11.echo paginate_links($dynamicPaginationArgs);

12.}

After writing this page navigate to a search.php or archive.php

Then call the function to your desired area.

<div class = "pagination">

<?php dynamicPagination();?>

</div>

Code Explanation

The code relies on the paginate_links function to display the dynamic numeric pagination.

The function must take the total pages created by a specific search.

To get the total pages a global variable $wp_query provokes the variable max_num_pages.

This has been defined by line-2 and line-3.

Remember the paginate_links require a group of data for pagination.

The data can be passed by an array.

That is why line-5 creates an array given the name $dynamicPaginationArgs.

The array contains four elements each has a special work.

Element with Keyword ‘base’

The base element defines the website homepage address.

For example.

My blog address is http://webappguides.com/

The base specifies the URL address provided as the primary URL for all pages.

The keyword is assigned to a PHP function str_replace.

str_replace passes three data.

  1. Integer
  2. A formatted string (%#%)
  3. get_pagenum_link

The function get_pagenum_link retrieves links of numbers that appear in the pagination.

Element with Keyword ‘format’

WordPress sends a searched keyword using a special format.

For example, write the term CAT on the search box.

When you place enters a search address looks like this http://webappguides.com/?s=CAT

The same setup must be included in the dynamic pagination.

This is defined by Line-7

Element with Keyword ‘current’

The keyword let the system identifies the current viewing page

Element with Keyword ‘total’

As you have already know the total number of pages must be specified.

It can be set by element total in an array.