Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2012, 11:06 PM   PM User | #1
lukeman72
New to the CF scene

 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
lukeman72 is an unknown quantity at this point
Merging Functionality of Two Functions

I have two pieces of code, one that impacts wp_link_pages and another that impacts custom_link_pages. Instead, I need both codes to impact wp_link_pages.

I know to do this I need to use my add_filter('wp_link_pages_args', 'wp_link_pages_args_prevnext_add') code and pass the same arguments to custom_link_pages as I did to wp_link_pages, but I'm not doing it properly when I try - so if somebody knows how and could paste the code I'd be forever grateful!

To make this clearer:

- The first code allows the wp_link_pages to display both page numbers and next/previous links, which WordPress doesn't allow by default
- The second code turns the current/active page number in wp_link_pages into a link (the current/active page number is not a link by default in WordPress) but it must be called using custom_link_pages instead of wp_link pages.
- I would like to create a code which does both of these (allows wp_link_pages to display both previous/next links and page numbers, and turn the current/active page number into a link) to wp_link_pages

An example of the result I'm looking for can be found at the bottom of this link.

I've tried this every which way that I know how (which are apparently the improper ways) and have received nothing but errors.

MY WP_LINK_PAGES CODE
Code:
    // 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;
}
MY CUSTOM_LINK_PAGES CODE

Code:
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;
}
lukeman72 is offline   Reply With Quote
Reply

Bookmarks

Tags
functions, php, wordpress

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:48 AM.


Advertisement
Log in to turn off these ads.