Add Custom WordPress Pagination Without a Plugin

Add Custom WordPress Pagination Without a Plugin
808 Views
0
(0)

One of the most popular pagination options for blog articles and custom post types is to create custom query pagination in WordPress. This essay will walk you through the process of adding custom query pagination to your WordPress site.

In most circumstances, pagination is really inconvenient. advertisement is one of the worst-case possibilities. It also has a significant impact on SEO. However, when it comes to page load time and user experience, pagination will be your best friend. Many websites advocate using pagination to divide hundreds of posts or pages into multiple pages.

What is WordPress pagination?

For example, if you have 100 blog articles, you could adjust your pagination settings to display 10 blog posts per page, dividing your blog post list into 10 pages with 10 items each.

Visitors may then move between the pages that list their entries by clicking the “Next” or “Previous” buttons. In some circumstances, visitors will receive a numbered list of pages from which they can select one.

Use this code for WordPress pagination

<?php
function cxc_custom_pagination($numpages = '', $pagerange = '', $paged='') {

	if (empty($pagerange)) {
		$pagerange = 2;
	}

	global $paged;

	if (empty($paged)) {
		$paged = 1;
	}

	if ($numpages == '') {
		global $wp_query;
		$numpages = $wp_query->max_num_pages;
		if(!$numpages) {
			$numpages = 1;
		}
	}

	$pagination_args = array(
		'base'            => get_pagenum_link(1) . '%_%',
		'format'          => 'page/%#%',
		'total'           => $numpages,
		'current'         => $paged,
		'show_all'        => False,
		'end_size'        => 1,
		'mid_size'        => $pagerange,
		'prev_next'       => True,
		'prev_text'       => __('«'),
		'next_text'       => __('»'),
		'type'            => 'array',
		'add_args'        => false,
		'add_fragment'    => ''
	);

	$paginate_links = paginate_links( $pagination_args );

	if (is_array($paginate_links)) {
		echo "<div class='wc-pagination'>";
		echo '<ul class="pagination">';
		foreach ( $paginate_links as $page ) {
			echo "<li>$page</li>";
		}
		echo '</ul>';
		echo "</div>";
	}
}

add_shortcode( 'show_all_post', 'cxc_display_all_post_list' );
function cxc_display_all_post_list(){
	ob_start();

	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$args = array(
		'paged' => $paged, // pass paged parameter
		'post_type'=> 'post',
		'orderby'    => 'ID',
		'post_status' => 'publish',
		'order'    => 'DESC',
		'posts_per_page' => 4
	);
	$result = new WP_Query( $args );
	if ( $result-> have_posts() ) { 
		echo "<ul>";
		while ( $result->have_posts() ) { 
			$result->the_post(); 
			echo "<li>".get_the_title()."</li>";
		}
		echo "<ul>";
	}
	cxc_custom_pagination($result->max_num_pages,"", $paged); // call pagination function after post loop
	wp_reset_postdata();

	return ob_get_clean();
}
?>

Last thoughts

Pagination allows you to divide the content of your WordPress site into numerous pages. This can help people and search engines browse more easily, and it can also enhance speed by loading less data on each page.

All WordPress sites have pagination functionality by default. However, most themes’ default pagination system is restricted.

Do you have any more questions concerning WordPress pagination? Please leave your comments in the section below!

How useful was this blog?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this blog.

Leave a comment

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