...

View Full Version : Merging Functionality of Two Functions



lukeman72
01-25-2012, 12:06 AM
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! :thumbsup:

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 (http://www.menshealth.com/celebrity-fitness/garrett-hedlund-workout).

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

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


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum