17

I want wp_link_pages (mutli-page posts) to display the page numbers, the word "previous" before those numbers, and a "next" after those numbers. It would look like this:

Prev 1, 2, 3, 4 Next

I'm attempting to do this without a plugin. Here's what I've tried so far, but it isn't working, it is only displaying numbers.

<?php wp_link_pages(array(
    'before' => '<span style="clear:both; display:block">Pages', 
    'after'  => '</span>',
    'next_or_number'=>'number',
    'previouspagelink' => 'previous',
    'nextpagelink'=> 'Next'
)); ?>

6 Answers 6

14
+50

The function you're using, wp_link_pages­Codex, does not have the feature you're looking for by default.

However you can easily extend it by using a callback function, registered as a filter on that functions arguments:

add_filter('wp_link_pages_args', 'wp_link_pages_args_prevnext_add');

The filter will then modify the parameters that are used in that function on-the-fly and inject the missing links to the prev and next arguments which are output on the left and right side of the numbered link list (next_or_number' => 'number'):

/**
 * Add prev and next links to a numbered page link list
 */
function wp_link_pages_args_prevnext_add($args)
{
    global $page, $numpages, $more, $pagenow;

    if ($args['next_or_number'] !== 'next_and_number') 
        return $args; # exit early

    $args['next_or_number'] = 'number'; # keep numbering for the main part
    if (!$more)
        return $args; # exit early

    if ($page - 1) # there is a previous page
        $args['before'] .= _wp_link_page($page-1)
            . $args['link_before']. $args['previouspagelink'] . $args['link_after'] . '</a>'
        ;

    if ($page < $numpages) # there is a next page
        $args['after'] = _wp_link_page($page+1)
            . $args['link_before'] . ' ' . $args['nextpagelink'] . $args['link_after'] . '</a>'
            . $args['after']
        ;

    return $args;
}

Usage:

wp_link_pages(array(
    'before' => '<p>' . __('Pages:'),
    'after' => '</p>',
    'next_or_number' => 'next_and_number', # activate parameter overloading
    'nextpagelink' => __('Next'),
    'previouspagelink' => __('Previous'),
    'pagelink' => '%',
    'echo' => 1 )
);

If your theme always uses the prev and next links, then you could change the function a little to make it the default behaviour whenever number (the default) is used, so you need to write less in your themes.

This is a slightly different implementation as suggested by Velvet Blues.

5
  • Except for a few comments and early returns, the implementation, option name, and explanation is exactly the same as mine. Am I overlooking something? What benefit does your solution give? Thanks. Dec 31, 2011 at 18:26
  • Probably when I compare the code.
    – hakre
    Dec 31, 2011 at 18:34
  • Hakre’s solution as available as long as the question is here. Complete answers (= understandable without relying on external sources) are always preferred on StackExchange.
    – fuxia
    Dec 31, 2011 at 18:40
  • The difference is that @hakre introduced an error. In this line of code: if (!$args['next_or_number'] == 'next_and_number') he's missing brackets, so $args is negated before its logical evaluation, it should be: if (!($args['next_or_number'] == 'next_and_number')) or better if($args['next_or_number']!='next_and_number')
    – deviato
    Apr 1, 2021 at 16:53
  • @deviato: thanks for the remark, adopted, also with strict comparison.
    – hakre
    Apr 2, 2021 at 7:37
6

The wp_link_pages() function only shows either text or number, never both. If you take a look at the function's code, you'll see that there is no option to make it behave differently by passing parameters.

That being said, there are three ways to do this without a plugin:

  1. Create your own function in your theme's functions.php file. Works, but then you'd have a new function.
  2. Use 2-3 instances of wp_link_pages(). Very inefficient hack.
  3. Use a filter. Preferred Method. See below.

I've written an article on how to do this on my blog. Basically, I use the wp_link_pages_args filter and add a function in the functions.php file which adds a new option 'next_and_number'.

WordPress Hack: Display Number & Next/Previous Links with wp_link_pages()

1
  • Cool man... I was thinking of something using the 4 global values, but never really got down to a solution... +1! Dec 30, 2011 at 16:59
3

Try this, you can more customize it. But it should do as you wanted :-)

function tp_link_pages() {
    global $page, $numpages;

    echo paginate_links( array(
        'format' => get_permalink() . '%#%/',
        'current' => $page,
        'total' => $numpages
    ) );
}
1
1

I don't get what's the problem... Do you have any error?

This should work:

<?php $args = array(
    'before'           => '<span style="clear:both; display:block">Pages',
    'after'            => '</span>',
    'nextpagelink'     => __('Next'),
    'previouspagelink' => __('Previous')
); 

wp_link_pages($args);
?> 

You don't need to add next_or_number as number is already the default.

3
  • For some reason this isn't working, but it seems right to me. I wonder if there was a change to 3.3 or if I need to paste something into functions.php? Dec 28, 2011 at 14:55
  • 2
    Yes I tried this too, it works when you set next_or_number to next. But it shows only the 'Next' and 'Previous' links, not the numbers! Dec 28, 2011 at 16:53
  • 1
    I need both, as shown in the example. Dec 28, 2011 at 17:20
1

This code is on the loop of posts? The coding is OK to me. Here is the Codex example on how to use:

<?php

wp_link_pages(array(
    'before' => '<p>' . __('Pages:'),
    'after' => '</p>',
    'next_or_number' => 'number',
    'nextpagelink' => __('Next page'),
    'previouspagelink' => __('Previous page'),
    'pagelink' => '%',
    'echo' => 1 )
);

?>
3
  • This code does not work. Dec 28, 2011 at 17:19
  • Code fixed. Sorry. Some attributes are without a value.
    – Caio Alves
    Dec 28, 2011 at 19:41
  • 1
    This does not achieve the results I was looking for in my question, it just shows numbers. Dec 29, 2011 at 14:07
0

The answer is different, but it was inspired @荒野无灯 who decided to delete his answer. Imho it's the best, as the most easy and flexible solution:

The solution depends on paginate_links(). The only thing to know is that base will be the URl from the start, appended by %_%, which will then be replaced by format. So as long as we use get_permalink().'%_%';, we know that we will in any case stay on the current post. Inside format, the # gets replaced by the page number:

function wpse37256_paginate_paged()
{
    if ( ! $paginate_links = paginate_links( array(
         'type'      => 'array'

        ,'total'     => $GLOBALS['numpages']
        ,'mid_size'  => 1
        ,'end_size'  => 1

        #,'prev_next' => false
        ,'prev_text' => '&laquo; '.__( 'Prev', 'your_textdomain' )
        ,'next_text' => __( 'Next', 'your_textdomain' ).' &raquo;'

        ,'base'      => get_permalink().'%_%'
        ,'format'    => user_trailingslashit( '%#%' )

        ,'current'   => $GLOBALS['page']
    ) ) )
        return;

    echo "<div class='pagination-container'>{$paginate_links}</div>";
}
0

Your Answer

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

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