ayumee
9/22/2017 - 4:49 PM

Adding pagination to page template

Build the all post archive page with patination on wordpress page template ( with bootstrap )

Reference : https://gist.github.com/hissy/6103177

<?php
/**
 * Template Name: Archive Page with bootstrap */
?>

<?php
	$paged = (int) get_query_var('paged');
	$args = array(
		'posts_per_page' => 5,
		'paged' => $paged,
		'orderby' => 'post_date',
		'order' => 'DESC',
		'post_type' => 'post',
		'post_status' => 'publish'
	);
	$the_query = new WP_Query($args);

	if ( $the_query->have_posts() ) :

		while ( $the_query->have_posts() ) : $the_query->the_post();
			get_template_part( 'template-parts/content', get_post_format() );
		endwhile;

	else:
		get_template_part( 'template-parts/content', 'none' );
	endif;

	if ($the_query->max_num_pages > 1) {

		$args = array(
			'base' => get_pagenum_link(1) . '%_%',
			'format' => 'page/%#%/',
			'current' => max(1, $paged),
			'total' => $the_query->max_num_pages
		);

		$navigation = '';

		$links = paginate_links( $args );
		if ( $links ) {

			$links = preg_replace("/&laquo; Previous/", "<i class='fa fa-angle-left'></i> Previous", $links);

			$links = preg_replace("/Next &raquo;/", "Next <i class='fa fa-angle-right'></i>", $links);

			$links = preg_replace("/<a/", "<li class='page-item'><a", $links);
			$links = preg_replace("/<\/a>/", "</a></li>", $links);

			$links = preg_replace("/<span/", "<li class='page-item'><span", $links);
			$links = preg_replace("/<\/span>/", "</span></li>", $links);

			$links = preg_replace("/page-numbers/", "page-link", $links);

			$links = preg_replace("/<li class='page-item'><span class='page-link current/", "<li class='page-item active'><span class='page-link", $links);

			$template = '<ul class="pagination pagination-lg justify-content-center">%1$s</ul>';
			$navigation = sprintf($template, $links);
		}

		echo $navigation;

	}

	wp_reset_postdata( $args );
?>
<?php
/**
 * Template Name: Archive Page */
?>

<?php
$paged = (int) get_query_var('paged');
$args = array(
	'posts_per_page' => 3,
	'paged' => $paged,
	'orderby' => 'post_date',
	'order' => 'DESC',
	'post_type' => 'post',
	'post_status' => 'publish'
);
$the_query = new WP_Query($args);

if ( $the_query->have_posts() ) :
	while ( $the_query->have_posts() ) : $the_query->the_post();
		get_template_part( 'content', get_post_format() );
	endwhile;
else:
	get_template_part( 'content', 'none' );
endif;

if ($the_query->max_num_pages > 1) {
	echo paginate_links(array(
		'base' => get_pagenum_link(1) . '%_%',
		'format' => 'page/%#%/',
		'current' => max(1, $paged),
		'total' => $the_query->max_num_pages
	));
}

wp_reset_postdata();
?>