wordpress自定义翻页分页,把如下代码放到模板所在目录的functions.php里即可:
/** * @author albert * @date 2021-06-09 * 数字分页函数 * 因为wordpress默认仅仅提供简单分页 * 所以要实现数字分页,需要自定义函数 * @Param int $range 数字分页的宽度 * @Return string|empty 输出分页的HTML代码 */ function albert_pagination( $range = 4 ) { global $paged,$wp_query; if ( !$max_page ) { $max_page = $wp_query->max_num_pages; } if( $max_page >1 ) { echo "<div class='fenye'>"; if( !$paged ){ $paged = 1; } if( $paged != 1 ) { echo "<a href='".get_pagenum_link(1) ."' class='extend' title='Go to home' rel='nofollow'>Home</a>"; } previous_posts_link('Previous'); if ( $max_page >$range ) { if( $paged <$range ) { for( $i = 1; $i <= ($range +1); $i++ ) { echo "<a rel='nofollow' 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 rel='nofollow' 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 rel='nofollow' href='".get_pagenum_link($i) ."'";if($i==$paged) echo " class='current'";echo ">$i</a>"; } } }else{ for($i = 1;$i <= $max_page;$i++){ echo "<a rel='nofollow' href='".get_pagenum_link($i) ."'"; if($i==$paged)echo " class='current'";echo ">$i</a>"; } } next_posts_link('Next'); if($paged != $max_page){ echo "<a rel='nofollow' href='".get_pagenum_link($max_page) ."' class='extend' title='Go to last'>Last</a>"; } echo '<span class="ml-3">Total<span class="mx-1">[ '.$max_page.' ]</span>page</span>'; echo "</div>\n"; } }
上一篇: 《建站管家》二次开发文档