ワードプレス 記事一覧内に、プラグインを使わず、自由にカスタマイズ可能なページネーション(ページ送り・ページング)を追加する。
function.php
/*ページネーション*/ function pagination($pages = '', $range = 2, $anotate= false){ $showitems = ($range * 2) + 1; global $paged; if(empty($paged)) $paged = 1; if($pages == ''){ global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages){ $pages = 1; } } if(1 != $pages){ $html = ""; //先頭へ if($paged > 2 && $paged > $range+1 && $showitems < $pages){ $html .= '<a href="'.get_pagenum_link(1).'">« 先頭へ</a>'; } //最後へ if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages){ $html .= '<a href="'.get_pagenum_link($pages).'">最後へ »</a>'; } //前へ if($paged > 1/* && $showitems < $pages*/){ $html .= '<div class="pagenate_previous"><a href="'.get_pagenum_link($paged - 1).'" class="btn02">前へ</a></div>'; } //次へ if ($paged < $pages/* && $showitems < $pages*/){ $html .= '<div class="pagenate_next"><a href="'.get_pagenum_link($paged + 1).'" class="btn02">次へ</a></div>'; } $html .= '<ul>'; // ページリンクループ for ($i=1; $i <= $pages; $i++){ if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){ $html .= ($paged == $i)? '<li><span class="current">'.$i.'</span></li>':'<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>'; } } $html .= '</ul>'; echo $html; } }
※ "range"は現在のページから前後何件分表示するか。デフォルトは前後2件。合計5つのページリンクを表示する。"← 12③45 →"なイメージ
HTML(Wordpressテンプレートコード)例
<?php if ( have_posts() ) : ?> <?php echo ''.$wp_query->found_posts.' 件の検索結果'; ?> <?php if (function_exists("pagination")) { pagination(); } ?> <?php while (have_posts()) : the_post(); ?> <?php include( TEMPLATEPATH . '/entry_template.php' ); ?> <?php endwhile; ?> <?php if (function_exists("pagination")) { pagination(); } ?> <?php echo ''.$wp_query->found_posts.' 件の検索結果'; ?> <?php else : ?> <?php endif; ?>
« Mavic キシリウム SLRを買ったので自慢したい | ホーム | ウィンドウ下部に固定、スクロールでニョキっと出す方法 殴り書きその1 »
http://pulltab.info/mt/mt-tb.cgi/112