当前位置:Gxlcms > PHP教程 > Wordpress php 分页代码

Wordpress php 分页代码

时间:2021-07-01 10:21:17 帮助过:17人阅读

效果:

将下面的函数放到你的主题的 functions.php 文件中:
代码如下:

  1. <br>function theme_echo_pagenavi(){ <br>global $request, $posts_per_page, $wpdb, $paged; <br>$maxButtonCount = 9; //显示的最多链接数目 <br>if (!is_single()) { <br>if(!is_category()) { <br>preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches); <br>} else { <br>preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches); <br>} <br>$fromwhere = $matches[1]; <br>$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere"); <br>$max_page = ceil($numposts /$posts_per_page); <br>if(empty($paged)) { <br>$paged = 1; <br>} <br>$start = max(1, $paged - intval($maxButtonCount/2)); <br>$end = min($start + $maxButtonCount - 1, $max_page); <br>$start = max(1, $end - $maxButtonCount + 1); <br>if($paged == 1){ <br>echo "<span>首页</span>"; <br>echo "<span>上一页</span>"; <br>}else{ <br>echo '<a href="'.get_pagenum_link().'"><span>首页</span></a>'; <br>echo '<a href="'.get_pagenum_link($paged-1).'"><span>上一页</span></a>'; <br>} <br>for($i=$start; $i<=$end; $i++){ <br>if($i == $paged) { <br>echo "<span class=\"page_num on\">[$i]</span>"; <br>} else { <br>echo '<a href="'.get_pagenum_link($i).'"><span class="page_num">['.$i.']</span></a>'; <br>} <br>} <br>if($paged == $max_page){ <br>echo "<span>下一页</span>"; <br>echo "<span>末页</span> "; <br>}else{ <br>echo '<a href="'.get_pagenum_link($paged+1).'"><span>下一页</span></a>'; <br>echo '<a href="'.get_pagenum_link($max_page).'"><span>末页</span></a>'; <br>} <br>echo " 共{$numposts}条记录, {$max_page}页."; <br>} <br>} <br><br>在主题的 index.php 文件中这样引用: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php theme_echo_pagenavi(); ?> <br><p></p></li><li> </li><li> </li></ol></pre>

人气教程排行