对于我们做网站的来说,一般列表页文章一多都需要分页,下面华仔就来介绍下WordPress 怎么制作分页,直接上代码
代码放functions.php 里
function get_pagenavi($range = 3这里的3就是每页显示多少条信息,这个地方的数一定要和WordPress后台设置阅读里的博客页面至多显示多少条一定要一样,不然分页会报404){
global $paged, $wp_query;
if (!$max_page) {
$max_page = $wp_query->max_num_pages;
}
if ($max_page > 1) {
if (!$paged) {
$paged = 1;
}
echo "<li>"; previous_posts_link('<');echo"</li>";
if ($max_page > $range) {
if ($paged < $range) {
for ($i = 1; $i <= ($range + 1); $i++) {
echo "<li><a href='" . get_pagenum_link($i) . "'";
if ($i == $paged) echo " class='active'";
echo ">$i</a></li>";
}
} elseif ($paged >= ($max_page - ceil(($range / 2)))) {
for ($i = $max_page - $range; $i <= $max_page; $i++) {
echo "<li><a href='" . get_pagenum_link($i) . "'";
if ($i == $paged) echo " class='active'";
echo ">$i</a></li>";
}
} elseif ($paged >= $range && $paged < ($max_page - ceil(($range / 2)))) {
for ($i = ($paged - ceil($range / 2)); $i <= ($paged + ceil(($range / 2))); $i++) { echo "<li><a href='" . get_pagenum_link($i) . "'";
if ($i == $paged) echo " class='active'";
echo ">$i</a></li>";
}
}
} else {
for ($i = 1; $i <= $max_page; $i++) {
echo "<li><a href='" . get_pagenum_link($i) . "'";
if ($i == $paged)
echo " class='active'";
echo ">$i</a></li>";
}
}
echo "<li class=next>";
echo next_posts_link('>');
echo "</li>"; }}
前台调用代码 <?php get_pagenavi(); ?>
文章评论