Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

With the default wp_link_pages the current/active page number is not a link. I would like to find a code/hack which turns the current/active page into a link to the top of the page.

I am already using a custom code for wp_link_pages which can be found below:

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

In my functions:

// Custom Next/Previous Page
add_filter('wp_link_pages_args', 'wp_link_pages_args_prevnext_add');
/**
 * Add prev and next links to a numbered 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;
}
    ?>
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Doesn't look like there are proper hooks to modify the output as per your requirements. But you could write a function similar to wp_link_pages() and call that in your theme. The following function would do your trick.

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

        $r = wp_parse_args( $args, $defaults );
        $r = apply_filters( 'wp_link_pages_args', $r );
        extract( $r, EXTR_SKIP );

        global $page, $numpages, $multipage, $more, $pagenow;
        $output = '';
        if ( $multipage ) {
                if ( 'number' == $next_or_number ) {
                        $output .= $before;
                        for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
                                $j = str_replace('%',$i,$pagelink);
                                $output .= ' ';
                                if ( ($i != $page) || ((!$more) && ($page==1)) ) {
                                        $output .= _wp_link_page($i);
                                } elseif ( $i == $page ) {
                                    $output .= '<a href="#">';
                                }
                                $output .= $link_before . $j . $link_after;
                                if ( ($i != $page) || ( $i == $page ) || ((!$more) && ($page==1)) )
                                        $output .= '</a>';
                        }
                        $output .= $after;
                } else {
                        if ( $more ) {
                                $output .= $before;
                                $i = $page - 1;
                                if ( $i && $more ) {
                                        $output .= _wp_link_page($i);
                                        $output .= $link_before. $previouspagelink . $link_after . '</a>';
                                }
                                $i = $page + 1;
                                if ( $i <= $numpages && $more ) {
                                        $output .= _wp_link_page($i);
                                        $output .= $link_before. $nextpagelink . $link_after . '</a>';
                                }
                                $output .= $after;
                        }
                }
        }

        if ( $echo )
                echo $output;

        return $output;
}

You can use this function in the same way as wp_link_pages().

EDIT

To include both page numbers and next/previous links you should even add this filter to your functions.php.

// Custom Next/Previous Page
add_filter('wp_link_pages_args', 'wp_link_pages_args_prevnext_add');
/**
 * Add prev and next links to a numbered 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;
}

And to call the pagination you would put the following in you template or loop.

custom_link_pages(array(
    'before' => '<p class="pagelinks">' . __(''),
    'after' => '</p>',
    'next_or_number' => 'next_and_number', # activate parameter overloading
    'nextpagelink' => __('Next'),
    'previouspagelink' => __('Previous'),
    'pagelink' => '%',
    'echo' => 1 )
);
share|improve this answer
    
Am I supposed to add this in addition to what I have in my functions right now or replace what I have with this? Also, do I call it as I have been calling wp_link_pages in my original example? –  AndrettiMilas Jan 21 '12 at 21:45
    
You can replace your code with the code I have provided and instead of calling wp_link_pages() you can call custom_link_pages(). It also accepts the same arguments as wp_link_pages(). –  Joshua Abenazer Jan 22 '12 at 8:51
    
In my code in the original the code makes it so that both page numbers and next/previous links are displayed (usually WordPress). I'm trying to now combine these two codes but I'm not advanced enough at PHP to do so, would you be able to help me out with that? (And thanks!) –  AndrettiMilas Jan 23 '12 at 14:18
    
Ok you just need to use your add_filter('wp_link_pages_args', 'wp_link_pages_args_prevnext_add') code thats it, and pass the same arguments to custom_link_pages as you had passed to wp_link_pages –  Joshua Abenazer Jan 24 '12 at 5:41
    
I'm sorry, but I honestly have no idea what that means or how to do it. :-S –  AndrettiMilas Jan 24 '12 at 15:38
show 2 more comments

A simple way I'm using now, is to use 'link_before' and 'link_after' as part of the wp_link_pages args. You can then wrap each number, including the active one, in a tag, then style appropriately.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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