wordpress上一页下一页无插件分页函数
提供两个,一个是willin提供的,一个则是今天我提供的,其实就都是简单的修改,但可应对不同需求的应用.
第一个可以定制显示wordpress显示上一页下一页,及统计页数及中间页数,代码如下:
- if ( !function_exists('pagenavi') ) {
- function pagenavi( $p = 5 ) {
- if ( is_singular() ) return;
- global $wp_query, $paged;
- $max_page = $wp_query->max_num_pages;
- if ( $max_page == 1 ) return;
- if ( emptyempty( $paged ) ) $paged = 1;
- echo '<span class="pages">Page: ' . $paged . ' of ' . $max_page . ' </span> ';
- if ( $paged > 1 ) p_link( $paged - 1, '上一页', '« Previous' );
- if ( $paged > $p + 1 ) p_link( 1, '最前页' );
- if ( $paged > $p + 2 ) echo '... ';
- for( $i = $paged - $p; $i <= $paged + $p; $i++ ) {
- if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span class='page-numbers current'>{$i}</span> " : p_link( $i );
- }
- if ( $paged < $max_page - $p - 1 ) echo '... ';
- if ( $paged < $max_page - $p ) p_link( $max_page, '最后页' );
- if ( $paged < $max_page ) p_link( $paged + 1,'下一页', 'Next »' );
- }
- function p_link( $i, $title = '', $linktype = '' ) {
- if ( $title == '' ) $title = "第 {$i} 页";
- if ( $linktype == '' ) { $linktext = $i; } else { $linktext = $linktype; }
- echo "<a class='page-numbers' href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a> ";
- }
- }
第二个就是今天主要分享的,只显示上一页下一页,可定制样式,实例代码如下:
- if ( !function_exists('pagenavi') ) {
- function pagenavi( ) {
- if ( is_singular() ) return;
- global $wp_query, $paged;
- $max_page = $wp_query->max_num_pages;
- if ( emptyempty( $paged ) ) $paged = 1;
- if ( $paged > 1 ) p_linkp( $paged - 1, '上一页' );
- if ( $paged == 1 ) p_linkp1( $paged );
-
- if ( $paged < $max_page ) p_linkn( $paged + 1,'下一页' );
- if ( $paged == $max_page ) p_linkp0( $paged );
- }
- function p_linkp( $i, $title = '', $linktype = '' ) {
- echo "<div class='pre-page page'><a href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a></div> ";
- }
- function p_linkn( $i, $title = '', $linktype = '' ) {
- echo "<div class='next-page page'><a href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a></div> ";
- }
- function p_linkp0() {
- echo "<div class='pre-page page nav0'></a></div> ";
- }
- function p_linkp1() {
- echo "<div class='pre-page page nav1'></a></div> ";
- }
- }