wordpress分页是用来切换文章的一个功能,添加了wordpress分页后,用户即可自由到达指定的页面数浏览分类文章,而这样的一个很简单功能却有很多朋友在用插件,小编一直相应着一句wordpress界的口号“追求速度,远离插件”,而今天介绍的正是wordpress分页代码的教程!
其实方法很简单,依然是运用到wordpress的funtions.php文件:
- //分页
- function par_pagenavi($range = 9){
- 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="page-numbers">'.第 . $paged .页 .(共 . $max_page .页). ' </span> ';
- global $paged, $wp_query;
- if ( !$max_page ) {$max_page = $wp_query->max_num_pages;}
- if($max_page > 1){if(!$paged){$paged = 1;}
- if($paged != 1){echo "<a href='" . get_pagenum_link(1) . "' class='extend' title='跳转到首页'> NO.1 </a>";}
- previous_posts_link(' « ');
- if($max_page > $range){
- if($paged < $range){for($i = 1; $i <= ($range + 1); $i++){echo "<a href='" . get_pagenum_link($i) ."'";
- if($i==$paged)echo " class='current'";echo ">$i</a>";}}
- elseif($paged >= ($max_page - ceil(($range/2)))){
- for($i = $max_page - $range; $i <= $max_page; $i++){echo "<a href='" . get_pagenum_link($i) ."'";
- if($i==$paged)echo " class='current'";echo ">$i</a>";}}
- elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
- for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){echo "<a href='" . get_pagenum_link($i) ."'";if($i==$paged) echo " class='current'";echo ">$i</a>";}}}
- else{for($i = 1; $i <= $max_page; $i++){echo "<a href='" . get_pagenum_link($i) ."'";
- if($i==$paged)echo " class='current'";echo ">$i</a>";}}
- next_posts_link(' » ');
- if($paged != $max_page){echo "<a href='" . get_pagenum_link($max_page) . "' class='extend' title='跳转到最后一页'> END </a>";}}
- }
代码放进去后,再到主题的style.css文件里添加样式:
- /*pagenavi*/
- .page_navi{width:100%;height: 34px;line-height: 34px;text-align:center;overflow:hidden;padding-top: 10px;position:relative;background: #F4F4F4;border-top: 1px solid #E8E8E8;}
- .page_navi a{display: inline-block;margin-right: 8px;height: 24px;line-height:24px;padding:0 8px;background:#e4e5e1;color: #626262;font-size:14px;}
- .page_navi a:hover,.page_navi .current{color:#fff;background: #8c9fcc url(jb.png) no-repeat center top;text-decoration: none;}
- .page_navi .page-numbers{position: absolute;top: 5px;left: 15px;letter-spacing: 1px;font-family: 微软雅黑;color: #424242;}
到了这步,功能是有了,但还要到前端加上调用,即添加调用代码至主题index.php、archive.php、category.php、search.php:
- <div class="page_navi"><?php par_pagenavi(9); ?></div>
9的含义是您要显示的页面熟,可以自由设置数值大小,样式也可以根据自己的喜欢修改!
这么样,是否真的那么简单呢?比用插件方便省心多了吧!!