Web Design and Development
WordPress' wp_link_pages() function, used for displaying page links in multi-page posts, lacks one big feature. You can display a list of individual page links OR you can display previous and next links, but you cannot display both.
In this article, I show you how to display both next/previous links and number links with wp_link_pages().1
In WordPress 3.0, a new filter, wp_link_pages_args, was added to allow easy customization of the arguments that are passed to wp_link_pages(). We will use that filter to add previous and next links to the before and after parameters.
First, we add the filter to our theme's functions.php file.
add_filter('wp_link_pages_args',add_next_and_number');
Then we write our function add_next_and_number().
function add_next_and_number($args){
if($args['next_or_number'] == 'next_and_number'){
global $page, $numpages, $multipage, $more, $pagenow;
$args['next_or_number'] = 'number';
$prev = '';
$next = '';
if ( $multipage ) {
if ( $more ) {
$i = $page - 1;
if ( $i && $more ) {
$prev .= _wp_link_page($i);
$prev .= $args['link_before']. $args['previouspagelink'] . $args['link_after'] . '</a>';
}
$i = $page + 1;
if ( $i <= $numpages && $more ) {
$next .= _wp_link_page($i);
$next .= $args['link_before']. $args['nextpagelink'] . $args['link_after'] . '</a>';
}
}
}
$args['before'] = $args['before'].$prev;
$args['after'] = $next.$args['after'];
}
return $args;
}
To use the modified wp_link_pages() function, you can call it exactly as you did before, just use the new option next_and_number as shown below.
<?php
wp_link_pages(array(
'before' => '<p>' . __('Pages:'),
'after' => '</p>',
'next_or_number' => 'next_and_number',
'nextpagelink' => __('Next'),
'previouspagelink' => __('Previous'),
'pagelink' => '%',
'echo' => 1 )
);
?>
The output of the above code will look something like this:
![]()
The beauty of this approach is that the core functionality of wp_link_pages() is not changed. On pages where you don't want to display both types of links, you can continue to use the next or number options.
For more information on multi-page posts and the wp_link_pages() function, visit the WordPress Codex:
Feel free to comment or ask questions below.
Trackback URL for this entry:
http://www.velvetblues.com/web-development-blog/wordpress-number-next-previous-links-wp_link_pages/trackback/
Contact us today for a quote. Click here to submit details regarding your project.
If you are making a general inquiry, send an email to info@velvetblues.com