WordPress nextpage display Previous 2 of 10 Next

I’m using wp_link_pages() to split my post into multiple pages. I want to achieve something like this as shown in the image below. Instead of showing all the page numbers with links, I want it to show only this format (Current Page of Total Page). Please help me with the exact codes. Thank you so much in advance.

Screenshot

Read More

PS: I don’t want to use plugins.

Related posts

Leave a Reply

1 comment

  1. There is a simple solution to this problem. I’ve got this inspiration from source file where function wp_link_pages is defined.

    function page_pagination( $echo = 1 )
    {
    
        global $page, $numpages, $multipage, $more;
    
        if( $multipage ) { //probably you should add && $more to this condition.
            $next_text = "next";
            $prev_text = "prev";
    
            if( $page < $numpages ) {
                $next = _wp_link_page( $i = $page + 1 );
                $next_link = $next . $next_text . "</a>";
            }
    
            if( $i = ( $page - 1 ) ) {
                $prev = _wp_link_page( $i );
                $prev_link = $prev . $prev_text . "</a>";
            }
    
            $output = 
            "<div class="prev-next-page">"
            . $prev_link 
            . "<span class="page-counter">{$page} of {$numpages}</span>"
            . $next_link
            . "</div>";
    
        }
    
        if( $echo ){
            echo $output;
        }
    
        return $output;
    
    }
    

    If you need more freedom you can always adapt this function to suit your purposes. Include function into wordpress loop! Like this for example:

    if ( have_posts( ) ) {
        while ( have_posts( ) ) {
            the_post( ); 
            //rest of code
            the_content();
            page_pagination();
        } 
    } 
    

    As you can see, there is “private” function _wp_link_page used to solve your problem. I don’t like using “private” functions, but it solves our problem.