Wordpress 的 bootstrap4 两个分页函数
一:在主题 functions 文件 或者自定义目录并引入
<?php
function wp_bootstrap_4_pagination()
{
if (is_singular()) {
return;
}
global $wp_query;
/** Check number of pages **/
if ($wp_query->max_num_pages <= 1) {
return;
}
$paged = get_query_var('paged') ? absint(get_query_var('paged')) : 1;
$max = intval($wp_query->max_num_pages);
/** Add current page to the array */
if ($paged >= 1) {
$links[] = $paged;
}
/** Add the pages around the current page to the array */
if ($paged >= 3) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if (($paged + 2) <= $max) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="pagination-container"><ul class="pagination">' . "\n";
/** Previous Post Link */
if (get_previous_posts_link()) {
printf('<li class="page-item">%s</li>' . "\n", get_previous_posts_link());
}
/** Link to first page, plus ellipses if necessary */
if (!in_array(1, $links)) {
$class = 1 == $paged ? ' class="page-item active"' : ' class="page-item"';
printf('<li%s><a class="page-link" href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link(1)), '1');
if (!in_array(2, $links)) {
echo '<li>…</li>';
}
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort($links);
foreach ((array) $links as $link) {
$class = $paged == $link ? ' class="page-item active"' : ' class="page-item"';
printf('<li%s><a class="page-link" href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link($link)), $link);
}
/** Link to last page, plus ellipses if necessary */
if (!in_array($max, $links)) {
if (!in_array($max - 1, $links)) {
echo '<li>…</li>' . "\n";
}
$class = $paged == $max ? ' class="page-item active"' : ' class="page-item"';
printf('<li%s><a class="page-link" href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link($max)), $max);
}
/** Next Post Link */
if (get_next_posts_link()) {
printf('<li class="page-item">%s</li>' . "\n", get_next_posts_link());
}
echo '</ul></div>' . "\n";
}
/*
* Custom Attribute for links
*/
add_filter('next_posts_link_attributes', 'wp_boostrap_4_pagination_posts_link_attributes');
add_filter('previous_posts_link_attributes', 'wp_boostrap_4_pagination_posts_link_attributes');
function wp_boostrap_4_pagination_posts_link_attributes()
{
return 'class="page-link"';
}
在模板中使用
<?php wp_bootstrap_4_pagination();?>
二:
function ribs_pagenavi($before = '', $after = '', $p = 1)
{
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 . '<ul class="pagination mb-0">' . "\n";
if ($paged == 1) echo '<li class="page-item disable prev"><a class="page-link">上一页</a></li>';
if ($paged > 1) echo "<li class='page-item prev'><a class='page-link' href='", esc_html(get_pagenum_link($paged - 1)), "' title='上一页'>上一页</a></li>";
if ($paged > $p + 1) p_link(1, '首页');
if ($paged > $p + 2) echo '<li class="page-item disable"><a class="page-link">...</a></li>';
for ($i = $paged - $p; $i <= $paged + $p; $i++) {
if ($i > 0 && $i <= $max_page) $i == $paged ? print "<li class='page-item active'><span class='page-link'>{$i}<span class='sr-only'>(current)</span></span></li>" : p_link($i);
}
if ($paged < $max_page - $p - 1) echo '<li class="page-item disable"><a class="page-link">...</a></li>';
if ($paged < $max_page - $p) p_link($max_page, '尾页');
if ($paged < $max_page) echo "<li class='page-item next'><a class='page-link' href='", esc_html(get_pagenum_link($paged + 1)), "' title='下一页'>下一页</a></li>";
if ($paged == $max_page) echo '<li class="page-item disable next"><a class="page-link">下一页</a></li>';
echo '</ul>' . $after . "\n";
}
function p_link($i, $title = '', $linktype = '')
{
if ($title == '') $title = "第 {$i} 页";
if ($linktype == '') {
$linktext = $i;
} else {
$linktext = $linktype;
}
echo "<li class='page-item'><a class='page-link' href='", esc_html(get_pagenum_link($i)), "' title='{$title}'>{$linktext}</a></li>";
}
在模板中使用
<?php ribs_pagenavi('<nav class="post-pagenavi my-5">', '</nav>', 1); ?>