Simple WordPress Pagination

If you would like to add your own paging code in WordPress then the following post explains how to do it in the simplest way possible. Add the code provided below in their respective files and with a bit of tweaking you should have paging activated and running on your website.

// First Add In functions.php

function paginate() {
 global $wp_query, $wp_rewrite;
 $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

$pagination = array(
 'base' => @add_query_arg('page','%#%'),
 'format' => '',
 'total' => $wp_query->max_num_pages,
 'current' => $current,
 'show_all' => true,
 'type' => 'list',
 'next_text' => '»',
 'prev_text' => '«'
 );

if( $wp_rewrite->using_permalinks() )
 $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );

if( !empty($wp_query->query_vars['s']) )
 $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

echo paginate_links( $pagination );
 }

// then add the following code according to requirement

if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }

$temp = $wp_query; 
 $wp_query = null; 
 $args = array('post_type' => 'reviews', 'posts_per_page' => 1, 'paged' => $paged);
 $wp_query = new WP_Query();
 $wp_query->query( $args );

 while ($wp_query->have_posts()) : $wp_query->the_post();
 $comments_count = wp_count_comments();

  // Code you wish to place in the loop goes here

endwhile;

// Don’t Forget to add this following code at the End of page where you want to display pagination

Please follow and like us:

Be the first to comment

Leave a Reply

Your email address will not be published.


*