I'm currently in the process of learning PHP, and (even following tutorials) I can not figure out how to combine the two codes below. I'm using these on a WP install.

The first code currently affects wp_link_pages and the second creates custom functionality for custom_link_pages. I need the first to also affect custom_link_pages and combine the two codes.

The first code allows WordPress to use both page numbers and next/previous links in posts. The second code turns the the current/active page (which is not a link by default) into a link, I am using the second code to link to the top of the page when all is said and done..

First code, allows both page numbers and next/previous links:

// 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;
}

Second code, turns non-linked current/active page numbers into links:

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;
}
    Write a Reply...