pagination wordpress – How to make PHP wordpress Pagination With Example and Demo

pagination wordpress : WordPress Loop with Simple Pagination Code, Numeric Pagination, Ajax Pagination Example, Pagination for Custom Post Type and Custom Query Pagination WordPress and custom pagination function.

pagination wordpress – How To Add Pagination To WordPress?

Best WordPress Pagination Code Snippets with Examples

WordPress Loop with Simple Pagination Code

Simple pagination wordpress Code
wordpress pagination code for posts

<?php if ( have_posts() ) : ?>

<!-- Add the pagination functions here. -->

<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post();  ?>

<!-- the rest of your theme's main loop -->

<?php endwhile; ?>
<!-- End of the main loop -->

<!-- Add the pagination functions here. -->

<div class="nav-previous alignleft"><?php previous_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php next_posts_link( 'Newer posts' ); ?></div>

<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

WordPress Numeric Pagination

best pagination wordpress

<?php 

#STEP 1: Create the numeric WordPress pagination function 

function pakainfo_numeric_pagination() {
 
    if( is_singular() )
        return;
 
    global $wp_query;
 
    if( $wp_query->max_num_pages <= 1 )
        return;
 
    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );
 
    if ( $paged >= 1 )
        $permaurls[] = $paged;
 
    if ( $paged >= 3 ) {
        $permaurls[] = $paged - 1;
        $permaurls[] = $paged - 2;
    }
 
    if ( ( $paged + 2 ) <= $max ) {
        $permaurls[] = $paged + 2;
        $permaurls[] = $paged + 1;
    }
 
    echo '<div class="navigation"><ul>' . "\n";
 
    if ( get_previous_posts_link() )
        printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
 
    if ( ! in_array( 1, $permaurls ) ) {
        $class = 1 == $paged ? ' class="active"' : '';
 
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
 
        if ( ! in_array( 2, $permaurls ) )
            echo '<li>…</li>';
    }
 
    sort( $permaurls );
    foreach ( (array) $permaurls as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }

    if ( ! in_array( $max, $permaurls ) ) {
        if ( ! in_array( $max - 1, $permaurls ) )
            echo '<li>…</li>' . "\n";
 
        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }
 
    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<li>%s</li>' . "\n", get_next_posts_link() );
 
    echo '</ul></div>' . "\n";
 
}

#STEP 2: Add to the templates using this call to the function above 

pakainfo_numeric_pagination();

#STEP 3 : Style the Pagination appropriately to fit your theme styles 

/** CSS Classes to Style the Pagination*/ 

.navigation li {
    
}
.navigation li a{
	
	
}

WordPress Ajax Pagination Example

pagination in wordpress plugin

<?php
/**
 * Plugin Name:       Best Pakainfo Ajax Pagination
 * Plugin URI:        https://pakainfo.com 
 * Description:       Best WordPress Ajax pagination example.
 * Version:           1.0.0
 * Author:            Pakainfo
 * Author URI:        https://www.pakainfo.com 
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       cs-pagination
 * Domain Path:       /languages
 */


class Pakainfo_Ajax_Pagination {

	protected $num_per_page;

	public function __construct() {
		$this->num_per_page = 5;
		$this->init();
	}

	protected function init() {
		add_action( 'init', array( $this, 'add_rewrite_rule' ) );
		add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
		add_action( 'parse_request', array( $this, 'parse_request' ) );
	}

	function parse_request( $wp ) {

		if ( array_key_exists( 'cs-page', $wp->query_vars ) ) {

			$page = isset ( $wp->query_vars['cs-page'] ) ? $wp->query_vars['cs-page'] : 1;

			$args = array(
				'post_type'     => 'post',
				'post_per_page' => $this->num_per_page,
				'paged'         => $page,
			);

			$query  = new WP_Query( $args );

			$values['total'] = $query->found_posts;

			$values['per_page'] = $this->num_per_page;

			$values['posts'] = array();

			while ( $query->have_posts() ) {

				$query->the_post();

				$values['posts'][] = array(
					'ID'      => get_the_ID(),
					'title'   => get_the_title(),
				);
			}

			wp_reset_postdata();
			wp_send_json( $values );
		}
	}

	function add_query_vars( $query_vars ) {
		$query_vars[] = 'cs-page';

		return $query_vars;
	}

	function add_rewrite_rule() {
		add_rewrite_rule( '^cs-paginate/([0-9]+)/?', 'index.php?cs-page=$matches[1]', 'top' );
	}

	static function install() {
		flush_rewrite_rules();
	}
}


function pakainfo_ajax_pagination_init() {
	new Pakainfo_Ajax_Pagination();
}

pakainfo_ajax_pagination_init();

register_activation_hook( __FILE__, array( 'Pakainfo_Ajax_Pagination', 'install' ) );

WordPress Pagination for Custom Post Type

pagination in wordpress custom template

<?php 
 
 <?php

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

	$args = array(
		 'post_type' => 'custom_post_type_name',
		 'posts_per_page' => 10,
		 'paged' => $paged
	);

	$loop = new WP_Query( $args );

	while ( $loop->have_posts() ) : $loop->the_post();
 
 
endwhile;
?>
<nav class="pagination">
     <?php
     $big = 989889989;
     echo paginate_links( array(
          'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
          'format' => '?paged=%#%',
          'current' => max( 1, get_query_var('paged') ),
          'total' => $loop->max_num_pages,
          'prev_text' => '«',
          'next_text' => '»'
     ) );
?>
</nav>
<?php wp_reset_postdata(); ?>

don’t Miss : wordpress pagination example

Read Also:  php optional parameters - How to create optional arguments in PHP ?

Custom Query Pagination WordPress

<?php

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
 
$args = array(
    'posts_per_page' => 5,
    'category_name' => 'gallery',
    'paged' => $paged,
);
 
$the_query = new WP_Query( $args );
?>

<?php
 
$big = 999999999;
 
echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
) );

Our Latest Best WordPress Plugin

I hope you get an idea about pagination wordpress.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.