How to create a page /1 using <!--nextpage-->?

I am doing this on a post:

Content 1
!--nextpage--
Content 2
!--nextpage--
Content 3
!--nextpage--
Content 4
!--nextpage--

Which divides the content of my post/page into pages: Pages: 1 2 3 4, the URL of the post changes while you change of page:

mysite/post 
mysite/post/2
mysite/post/3
mysite/post/4

As you can see there is not mysite/post/1, if I want to access to page 1 changing the URL it redirects me to mysite/post/, so it seems they are the same.

However, I found a site that does something a bit different and I am wondering how they do it, this one, as you can see, you start on mysite/post and after click on the button the next "page" is mysite/post/1.

So it seems there is a page/0 before all that or that they change the slug of each page, whatever the case I'd love to know how can I achieve that behavior, unfortunately, I couldn't find much documentation about !--nextpage--

Topic nextpage customization Wordpress

Category Web


Parts of this are easy, and parts of this are not so easy. It depends on how complete of a solution you're looking for. Fixing the native pagination functions, for example, is probably more effort than it's worth. The problem is the fact that "page 0" and page 1 are the same page is baked into the code.

Anyway, the first step is to stop WordPress from redirecting from /post/1/ to /post/. This is fairly straightforward, using the redirect_canonical filter. Here we check if it's a singular post and the page number is 1. In that case, we return false to prevent the redirect, otherwise we return the redirect URL untouched.

function wpd_disable_page1_redirect( $redirect ) {
    if( is_singular('post') && ( 1 == get_query_var( 'page' ) ) ){
        return false;
    }
    return $redirect;
}
add_filter( 'redirect_canonical', 'wpd_disable_page1_redirect' );

You also have to modify the canonical URL that prints in the document head on the new "first page":

function wpd_page1_canonical( $canonical_url, $post ) {
    if( is_singular('post') && ( 1 == get_query_var( 'page' ) ) ){
        return $canonical_url . '1/';
    }
    return $canonical_url;
}
add_filter( 'get_canonical_url', 'wpd_page1_canonical', 10, 2 );

So now the page numbers work, but aside from the landing page, the wrong content is shown.

To fix that, we can use the content_pagination filter. This gives us access to the individual pages that were parsed from post content, in the form of an array. We can shuffle pages around, and add or remove them. The simplest fix for our purposes is to shift the whole array over by one element if we're on any page other than the landing page:

function wpd_content_pagination_filter( $pages, $post ){
    if( 1 < count( $pages ) && get_query_var( 'page' ) ){
        array_shift( $pages );
    }
    return $pages;
}
add_filter( 'content_pagination', 'wpd_content_pagination_filter', 10, 2 );

But this again highlights the underlying problem- we have a different number of pages getting returned in our array. We could do things a bit differently and fix that (by making it wrong in a different way), but there has to be a compromise somewhere, since lots of native functions think 0 and 1 are the same page.

There are certainly some details I've missed here, like handling "ugly" permalinks, a "phantom" last page you can manually paginate to, and, of course, pagination, but hopefully this should get you on your way.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.