WordPress post pagination without plugin

post-pagination

WordPress post pagination without plugin

There is lots of developers who actually didn’t know how to use pagination in WordPress posts. So many people are in this field are using plugin for WordPress posts pagination but they didn’t realize that plugin makes website heavy so if pagination can work by using some functions then why should we use plugin so let me tell you that how to use pagination in wordpress post without having any plugin. Lets start with some codes.

1) Use this method for default posts

Use this code into your fucntion.php file

if ( ! function_exists( 'post_pagination' ) ) :
   function post_pagination() {
     global $wp_query;
     $pager = 999999999; // need an unlikely integer

        echo paginate_links( array(
             'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
             'format' => '?paged=%#%',
             'current' => max( 1, get_query_var('paged') ),
             'total' => $wp_query->max_num_pages
        ) );
   }
endif;

After doing this call the pagination function where you want to display the pagination.

<?php post_pagination(); ?>

you have to set post limit from WordPress backend for this you have to go to Settings >> Reading >> Select number of posts you wish to display.

2) Use this method for custom posts

All the methods should be the same but when you are using custom post type you have to use this loop for pagination.

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

query_posts(array(
    'post_type' => 'post', // You can add a custom post type if you like
    'paged' => $paged,
    'posts_per_page' => 10 // limit of posts
));

if ( have_posts() ) :  while ( have_posts() ) : the_post(); 

     // post content goes here...

 endwhile; 

    post_pagination();

 else : 

// no posts found message goes here...

 endif;

By using this you can get pagination for WordPress posts without any plugin and your website will be load free. Hope this tutorial will be helpful to you and if you face any problem or you have any suggestion we welcomes you always.

Good Luck !



Leave a Reply

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