0

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-->

  • I tried clicking through to your example but it came back with "Sorry our content is not available in your area. We hope to be live in your area soon!!" – Tom J Nowell Aug 16 '18 at 19:22
  • Please check this out useloom.com/share/73f7504ee77540a9b851966954ec8709 – Castiblanco Aug 16 '18 at 19:27
  • That site probably doesn‘t use the nextpage tag, but attachment pages instead. It‘s not easy to mis-use nextpage like this. – swissspidy Aug 16 '18 at 20:22
  • I am pretty sure they use nextpage tag, or at least I think I am, if I view the code when I am on page 2, the body has a class single-paged-2, I think that class is added when using nextpage tag. – Castiblanco Aug 16 '18 at 21:05
1
+50

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.