让WordPress生成符合Bootstrap5样式的分页组件
May 9, 2023 [ 上次更新于 10 月前 ]
222 0 无人评论
因为WordPress的原生分页格式完全没办法匹配Bootstrap,所以寻思自己写一个,但是这部分因为不能像菜单一样直接添加walker,所以把原生分页的函数拿来改了改,但是这样原生分页函数中的一些钩子就用不了了,不过虽然好像也用不上了……
用法和原来是一样的,在需要loop的地方,替换WordPress原装的the_posts_pagination()
就好了。
不过记得写div样式,这个函数只是组装了列表出来。
如果需要让文章内也支持分页,则需要用wp_link_pages()
,在the_content()
之后加载,整个WP貌似只有这个函数会识别nextpage标记。
代码:
//分页函数
function aya_page_nav($type = ''){
//判断当前页面类型
if($type = 'post'){
global $paged,$wp_query;
$max_page = $wp_query->max_num_pages;
}else{
$count = wp_count_posts($type) -> publish;
$max_page = $count;
$paged = ceil($count / 10);
}
//定义最大显示分页数
$range = 5;
//拼接分页列表
if($max_page > 1) {
$html = '<ul class="pagination">';
if( !$paged ){
$paged = 1;
}
//上一页
if( $paged != 1 ) {
$html .= '<li class="page-item"><a class="page-link" href="'.get_pagenum_link(1).'"><span aria-hidden="true">首页</span></a></li>';
$html .= '<li class="page-item"><a class="page-link" href="'.get_pagenum_link($paged - 1).'"><span aria-hidden="true">上一页</span></a></li>';
}
//生成页码
if ( $max_page >$range ){
$range_page = ceil($range / 2);
$call_page = $max_page - $range_page;
if( $paged < $range ){
for( $i = 1; $i <= $range; $i++ ) {
$active = ($i == $paged) ? 'active' : '';
$html .= '<li class="page-item '.$active.'"><a class="page-link" href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}
}
elseif($paged >= $call_page){
for($i = ($max_page - $range); $i <= $max_page; $i++){
$active = ($i == $paged) ? 'active' : '';
$html .= '<li class="page-item '.$active.'"><a class="page-link" href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}
}
elseif($paged >= $range && $paged < $call_page){
for($i = ($paged - $range_page); $i <= ($paged + $range_page); $i++){
$active = ($i == $paged) ? 'active' : '';
$html .= '<li class="page-item '.$active.'"><a class="page-link" href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}
}
}else{
for($i = 1; $i <= $max_page; $i++){
$active = ($i == $paged) ? 'active' : '';
$html .= '<li class="page-item '.$active.'"><a class="page-link" href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}
}
//尾页
if($paged != $max_page){
$html .= '<li class="page-item"><a class="page-link" href="'.get_pagenum_link($paged + 1).'"><span aria-hidden="true">下一页</span></a></li>';
$html .= '<li class="page-item"><a class="page-link" href="'.get_pagenum_link($max_page).'"><span aria-hidden="true">尾页</span></a></li>';
}
//总页数
$html .= '<li class="page-item disabled"><a class="page-link">共 '.$max_page.' 页</a></li>';
$html .= '</ul>';
}
return $html;
}
站点声明:本站部分内容转载自网络,作品版权归原作者及来源网站所有,任何内容转载、商业用途等均须联系原作者并注明来源。
评论列表 0
暂无评论