无插件实现WordPress分页导航功能:
第一步,在当前主题下的functions.php中添加下面的代码:
/* Pagenavi */ function pagenavi( $before = '', $after = '', $p = 2 ) { if ( is_singular() ) return; global $wp_query, $paged; $max_page = $wp_query->max_num_pages; if ( $max_page == 1 ) return; if ( empty( $paged ) ) $paged = 1; echo $before.' <div id="pagenavi">'."\n"; echo '<span>Page: ' . $paged . ' of ' . $max_page . ' </span>'; if ( $paged > 1 ) p_link( $paged - 1, 'Previous Page', '«' ); if ( $paged > $p + 1 ) p_link( 1, 'First Page' ); 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, 'Last Page' ); if ( $paged < $max_page ) p_link( $paged + 1,'Next Page', '»' ); echo '</div> '.$after."\n"; } function p_link( $i, $title = '', $linktype = '' ) { if ( $title == '' ) $title = "Page {$i}"; if ( $linktype == '' ) { $linktext = $i; } else { $linktext = $linktype; } echo "<a class="page-numbers" title="{$title}" href="", esc_html( get_pagenum_link( $i ) ), "">{$linktext}</a>"; }
第二步,在当前主题下的样式style.css中添加下面的样式:
/* pagenavi */ #pagenavi a, #pagenavi a:visited, #pagenavi span { height: 25px; line-height: 25px; display: inline-block; padding: 1px 8px; } #pagenavi a, #pagenavi a:visited { margin: 0 2px; } #pagenavi span.pages { color: #777; font-weight: bold; margin-right: 10px; padding: 0; } #pagenavi span.current { margin: -2px 2px -1px; padding: 0 9px; height: 28px; line-height: 28px; text-align: center; }
如果自己不喜欢这样的分页样式我们可以修改成自己想要的。
最后一步,把下面的代码添加到模板的默认分页。
<?php pagenavi(); ?>
不错哦