How to implement paging in any page in wordpress

Let’s say our page name is abc and page id is 1234.

First we need to put the below code in functions.php file in your active theme.

//////////////////////////////////////////
function codeflask_page_rewrite() {
    add_rewrite_rule(‘^abc/page/([0-9]+)/?’, ‘index.php?page_id=1234&paged=$matches[1]’, ‘top’);
}
add_action(‘init’, ‘codeflask_page_rewrite’);
///////////////////////////////////////////

Now login to wp-admin, Goto Settings > Permalink and click on Save Changes.

Use the below code to get the page value on the abc page

////////////////////////////////////////////
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
////////////////////////////////////////////

Below is the sample code to display the data page wise:

<?php
$post_per_page=9;
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$off=$post_per_page*($paged-1);

////////////// Get Total Posts /////////////////
$cargs = array( ‘posts_per_page’ => -1, ‘offset’=> 0, ‘orderby’ => ‘ID’, ‘order’ => ‘DESC’, ‘post_type’ => ‘posts’, ‘post_status’ => ‘publish’);
$total_posts = get_posts($cargs);
$records=count($total_posts);
////////////// Get Total Posts /////////////////

$args = array( ‘posts_per_page’ => $post_per_page, ‘offset’=> $off, ‘orderby’ => ‘ID’, ‘order’ => ‘DESC’, ‘post_type’ => ‘posts’, ‘post_status’ => ‘publish’);
$posts_array = get_posts($args);

foreach($posts_array as $post_data){
    // Post ID
    $id = $post_data->ID;
    // Featured Image
    $featured_image = wp_get_attachment_url( get_post_thumbnail_id($post_data->ID) );
    // Post Title
    echo $post_data->post_title;
} ?>

<!——— Paging Display Code Starts here —————->
<div class=”custom_paging”>
    <?php
    $a=$records%$post_per_page;
    $f=floor($records/$post_per_page);
    if($a>0){
        $f++;
    }
    if($paged>1)
    echo ‘<a href=”/abc/page/’.($paged-1).'”><</a>’;
    for($j=1;$j<=$f; $j++){
        $c=””;
        if($j==$paged) $c=”active”;
        if($j==1){
            echo ‘<a class=”‘.$c.'” href=”/abc/”>’.$j.'</a>’;
        }else{
            echo ‘<a class=”‘.$c.'” href=”/abc/page/’.$j.'”>’.$j.'</a>’;
        }
}
    if($paged<$f)
    echo ‘<a href=”/abc/page/’.($paged+1).'”>></a>’;
    ?>
</div>
<!——— Paging Display Code Ends here —————->