Custom Search

News World

Oct 9, 2012

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


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().


Adding 'next_and_number' Option to wp_link_pages()

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'] . '
';
                }
                $i = $page + 1;
                if ( $i <= $numpages && $more ) {
                    $next .= _wp_link_page($i);
                    $next .= $args['link_before']. $args['nextpagelink'] . $args['link_after'] . '';
                }
            }
        }
        $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.


    wp_link_pages(array(
        'before' => '' . __('Pages:'),
        'after' => '
',
        'next_or_number' => 'next_and_number',
        'nextpagelink' => __('Next'),
        'previouspagelink' => __('Previous'),
        'pagelink' => '%',
        'echo' => 1 )
    );
?>

The output of the above code will look something like this:

No comments:

IT Conversations

Moneycontrol Latest News

Latest new pages on Computer Hope

Latest from Infoworld

Door Lock

Door Lock Import Top Door Lock from China Contact Quality Manufacturers Now