get_next_posts_page_link adds Inexistent directories

Using get_next_posts_page_link function to get url of paginated post's next page url, but it's adding directories to it that simply do not exist.

Here is the code where I use it and probably missing something somewhere

while (have_posts()) : the_post();
        the_content();
endwhile;
// echoing to see the actual url it would bring up if used in an anchor
echo get_next_posts_page_link();

And here are the urls

On page 1: mysitedomain.com/paginatedpage/page/2/ instead of mysitedomain.com/paginatedpage/2/

On page 2: mysitedomain.com/paginatedpage/2/page/2/ instead of mysitedomain.com/paginatedpage/3/

On page 3: mysitedomain.com/paginatedpage/3/page/2/ instead of mysitedomain.com/paginatedpage/4/

And so on...

Answers 1

  • If you want links to the next and previous pages on a singular page created with <!--nextpage--> then you're using the wrong function.

    get_next_posts_page_link(), as suggested by the name, is for getting the next page of posts and is intended for use on archives.

    To add the pagination links to a singular page, use wp_link_pages(), and use it inside the loop.

    while (have_posts()) : the_post();
        the_content();
    
        wp_link_pages();
    endwhile;
    

    By default it outputs page numbers, but if you want "Next" and "Previous" links, set the next_or_number argument to next:

    while (have_posts()) : the_post();
        the_content();
    
        wp_link_pages( array(
            'next_or_number' => 'next',
        ) );
    endwhile;
    

    See the documentation for more options for customising the output.


Related Questions