A lot of a site I'm creating for a client is going to be posts with different pages for posts like "Top 10 " etc. This means I need to paginate the posts to create different pages. The best way I've found to do this is by using <!--nextpage--> in the content to separate the post up and create the pages. To navigate through the pages, I've tried to use get_pagenum_link. There is here my full code:

function postPagination() {
    global $paged;
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    $output .= "<div class='pagination'>";
        $output .= "<a href='".get_pagenum_link($paged - 1)."'>Previous Page</a>";
        $output .= "<a href='".get_pagenum_link($paged + 1)."'>Next Page</a>";
    $output .= "</div>";
    return $output;

I have two main problems:

1) The links are in the form http://domain.dev/2014/03/03/post-title/2/page/2, which returns 404. Also, if I press the Next Page link, it will return /post-title/2/page/3, i.e the /page/3 part is correct but it's still on page 2. If i go to /post-title/page/3 or something, it will just redirect back to the first page. /post-title/3/ works fine. I'm assuming this is something to do with permalinks.

2) The Previous Page code doesn't work at all, it just returns the Current page link, e.g if you're on page 2, the Previous Page link will return /post-title/2/. i.e it doesn't even have the /page/2 bit on the end.

Sorry if that's confusing, happy to answer any questions and re-explain if necessary. I should also mention that this code is a function which is in functions.php and will be called in content pages.

Thanks for any help in fixing my two problems so the Previous/Next links work correctly:)

1 Answer 1


What you are doing seems to be reproducing what wp_link_pages() does:

Displays page-links for paginated posts (i.e. includes the Quicktag one or more times). This works in much the same way as link_pages() (deprecated), the difference being that arguments are given in query string format. This tag must be within The_Loop.

Using one or more of the provided arguments...

$defaults = array(
        'before'           => '<p>' . __( 'Pages:' ),
        'after'            => '</p>',
        'link_before'      => '',
        'link_after'       => '',
        'next_or_number'   => 'number',
        'separator'        => ' ',
        'nextpagelink'     => __( 'Next page' ),
        'previouspagelink' => __( 'Previous page' ),
        'pagelink'         => '%',
        'echo'             => 1

... you should be able to get pretty close to that markup.

In particular, I believe this will do it (untested):

$defaults = array(
        'before'           => '<div class='pagination'>',
        'after'            => '</div>',
        'nextpagelink'     => __( 'Next page' ),
        'previouspagelink' => __( 'Previous page' ),
  • thanks for the suggestion - this almost worked! Needed to add 'next_or_number => 'next'` to the arguments to make it use the nextpagelinkand previouspagelinkarguments, instead of just showing the page numbers. Thanks!
    – Tom Oakley
    Mar 3, 2014 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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