[WordPress]pagination

pagination
テーマのfunctions.phpに入れておくとよい。

function echo_pagination($params = array()){ 
	$params = array_merge(
		array(
			'per_page'       => 6,
			'link_count'     => 9,
			'left'           => '<li><a href="%url%">◀</a></li>',
			'right'          => '<li><a href="%url%">▶</a></li>',
			'format'         => '<li><a href="%url%">%no%</a></li>',
			'current_format' => '<li class="active"><a href="%url%">%no%</a></li>',
		),
		$params
	);
	global $paged;
	global $wp_query;
	if(empty($paged)){
		$paged = 1;
	}
	$pages = $wp_query->max_num_pages;
	if(!$pages){
		return;
	}

	if(1 < $paged){
		echo preg_replace('/%url%/',get_pagenum_link($paged-1),$params['left']);
	}

	$startPageNo = max(1, $paged-floor($params['link_count']*0.5));
	if($pages-$params['link_count'] < $startPageNo){
		$startPageNo = $pages-$params['link_count']+1;
	}
	for($i=$startPageNo;$i<$startPageNo+$params['link_count'] && $i <= $pages;$i++){
		$html = preg_replace('/%url%/',get_pagenum_link($i),$params[($i==$paged)?'current_format':'format']);
		echo preg_replace('/%no%/', $i, $html);
	}

	if($paged < $pages){
		echo preg_replace('/%url%/',get_pagenum_link($paged+1),$params['right']);
	}
}