之前大叔在wordpress主题下载站里写过《wordpress分页代码》的文章,而今天讲的意思大概一样,只是样式更加简洁,适合简约风格的人使用!下面就直接进入教程了,
在wordpress主题文件根目录下functions.php内加入以下代码:
- function par_pagenavi($range = 3){
- 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='跳转到首页'>«</a>";}
- 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(' »');
- }
- }
下面就是在需要分页的地方调用了
- <div id="page"><?php par_pagenavi(5); ?></div>
最后附加一些简单的css吧
- #page{width:100%;height:36px;line-height:36px;text-align:left;overflow:hidden;margin-left:auto;margin-right:auto;display:block;text-align:-moz-center;*text-align:center;text-align:center}
- #page a{text-decoration:none;color:#FFF;background:#282828;display:inline-block;padding:9px 12px;margin:0 5px 0 0;line-height:16px;margin-right:3px}
- #page a:hover,#page a.current{color:#FFF;background:#32a5e7}
OK大工高成,谢谢!