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'
)); ?>
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:
The filter will then modify the parameters that are used in that function on-the-fly and inject the missing links to the
prev
andnext
arguments which are output on the left and right side of the numbered link list (next_or_number' => 'number'
):Usage:
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.
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:
wp_link_pages()
. Very inefficient hack.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()
Try this, you can more customize it. But it should do as you wanted 🙂
I don’t get what’s the problem… Do you have any error?
This should work:
You don’t need to add next_or_number as number is already the default.
This code is on the loop of posts? The coding is OK to me. Here is the Codex example on how to use:
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 thatbase
will be the URl from the start, appended by%_%
, which will then be replaced byformat
. So as long as we useget_permalink().'%_%';
, we know that we will in any case stay on the current post. Insideformat
, the#
gets replaced by the page number: