wp-pagination

Custom Pagination without WordPress Plugins


On day 9 of our Advents Calendar we show a custom pagination function which allows a user to insert custom next, previous or numbered page links into a post. Use the follow code for an custom plugin or insert the function in your theme, inside the functions.php. Please see the second code-area for an example to hook inside the function for custom parameters.

Example Screenshots

The function

Now the function for the pagination

function fb_paging_bar( $args = array() ) {
	
	$defaults = array(
		'range'           => 4,
		'custom_query'    => FALSE,
		'previous_string' => __( '«««', FB_GREYFOTO_TEXTDOMAIN ),
		'next_string'     => __( '»»»', FB_GREYFOTO_TEXTDOMAIN ),
		'view_fp'         => TRUE,
		'view_lp'         => TRUE,
		'before_output'   => '<div class="postlink">',
		'after_output'    => '</div>'
	);
	
	$args = wp_parse_args( 
		$args, 
		apply_filters( 'fb_paging_bar_defaults', $defaults )
	);
	
	$args['range'] = (int) $args['range'] - 1;
	if ( !$args['custom_query'] )
		$args['custom_query'] = @$GLOBALS['wp_query'];
	$count = (int) $args['custom_query']->max_num_pages;
	$page  = intval( get_query_var( 'paged' ) );
	$ceil  = ceil( $args['range'] / 2 );
	
	if ( $count <= 1 )
		return FALSE;
	
	if ( !$page )
		$page = 1;
	
	if ( $count > $args['range'] ) {
		if ( $page <= $args['range'] ) {
			$min = 1;
			$max = $args['range'] + 1;
		} elseif ( $page >= ($count - $ceil) ) {
			$min = $count - $args['range'];
			$max = $count;
		} elseif ( $page >= $args['range'] && $page < ($count - $ceil) ) {
			$min = $page - $ceil;
			$max = $page + $ceil;
		}
	} else {
		$min = 1;
		$max = $count;
	}
	
	$echo = '';
	$previous = intval($page) - 1;
	$previous = esc_attr( get_pagenum_link($previous) );
	if ( $previous && (1 != $page) )
		$echo .= '<a href="' . $previous . '" title="' . __( 'previous', FB_GREYFOTO_TEXTDOMAIN) . '">' . $args['previous_string'] . '</a>';
	$firstpage = esc_attr( get_pagenum_link(1) );
	
	if ( $args['view_fp'] && $firstpage && (1 != $page) )
		$echo .= '<a href="' . $firstpage . '">' . __( 'First', FB_GREYFOTO_TEXTDOMAIN ) . '</a>';
	
	if ( !empty($min) && !empty($max) ) {
		for( $i = $min; $i <= $max; $i++ ) {
			if ($page == $i) {
				$echo .= '<span class="active">' . str_pad( (int)$i, 2, '0', STR_PAD_LEFT ) . '</span>';
			} else {
				$echo .= sprintf( '<a href="%s">%002d</a>', esc_attr( get_pagenum_link($i) ), $i );
			}
		}
	}
	
	if ($args['view_lp']) {
		$lastpage = esc_attr( get_pagenum_link($count) );
		if ( $lastpage && ($count != $page) ) {
			$count = str_pad( (int)$count, 2, '0', STR_PAD_LEFT );
			$echo .= '<a href="' . $lastpage . '">' . __( 'Last', FB_GREYFOTO_TEXTDOMAIN ) . '(' . $count . ')' . '</a>';
		}
	}
	
	$next = intval($page) + 1;
	$next = esc_attr( get_pagenum_link($next) );
	if ($next && ($count != $page) )
		$echo .= '<a href="' . $next . '" title="' . __( 'next', FB_GREYFOTO_TEXTDOMAIN) . '">' . $args['next_string'] . '</a>';
	
	if ( isset($echo) )
		echo $args['before_output'] . $echo . $args['after_output'];
}

For use the function inside the templates use the follow syntax:

<?php if ( function_exists('fb_paging_bar') ) fb_paging_bar(); ?>

Example for custom arguments

For custom arguments, please use the filter. Also an example for this solution.

function change_fb_paging_bar_defaults($args) {
	
	$args['previous_string'] = ''; // empty string
	$args['next_string'] = '';
	
	return $args;
}
add_filter('fb_paging_bar_defaults', 'change_fb_paging_bar_defaults');

Comments are closed.

3 comments

  1. Jay

    OR! Or... you can use the paginate_links function included in WordPress.. See Codex: http://codex.wordpress.org/Function_Reference/paginate_links

  2. Umbrovskis

    . __( 'Last', FB_GREYFOTO_TEXTDOMAIN ) . '(' . $count . ')' . '';

    should be an extra space: before count (' . $count . ')
    . __( 'Last', FB_GREYFOTO_TEXTDOMAIN ) . ' (' . $count . ')' . '';

  3. Frank

    @Jay: thanks for the link, i dont know this function - i will see, many thanks