我成功地实现了“最新帖子”页面的分页。但是我想在当前页之后分隔数字,并在它们和最后一页之间放置分隔符。下面是我要实现的html:
<div class="pagination-tt clearfix">
<ul>
<li><span class="deactive">1</span></li>
<li><a href="#" class="tbutton"><span>2</span></a></li>
<li><a href="#" class="tbutton"><span>3</span></a></li>
<li><span class="deactive">...</span></li>
<li><a href="#" class="tbutton"><span>8</span></a></li>
</ul>
<span class="pages">Page 1 of 8</span>
</div><!-- pagination -->
以下是php:
if (!function_exists(\'pagination\')){
function pagination($pages = \'\', $range = 2){
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == \'\'){
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages){
$pages = 1;
}
}
if(1 != $pages){
echo \'<div class="pagination-tt clearfix"><ul>\';
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo \'<li><a href="\'.get_pagenum_link(1).\'" class="tbutton"><span>«</span></a></li>\';
if($paged > 1 && $showitems < $pages) echo \'<li><a href="\'.get_pagenum_link($paged - 1).\'"><span>‹</span></a></li>\';
for ($i=1; $i <= $pages; $i++){
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
echo ($paged == $i)? \'<li><span class="deactive">\'.$i.\'</span></li>\':\'<li><a href="\'.get_pagenum_link($i).\'" class="tbutton">\'.$i.\'</a></li>\';
}
}
if ($paged < $pages && $showitems < $pages) echo \'<li><a href="\'.get_pagenum_link($paged + 1).\'">›</a></li>\';
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo \'<li><a href="\'.get_pagenum_link($pages).\'">»</a></li>\';
echo "</ul></div>\\n";
}
}
}
我如何编辑这个php以使其呈现上面显示的html?
最合适的回答,由SO网友:Amen Ra 整理而成
我深入研究了我编写的函数,并意识到了定制的灵活性。因此,我能够通过以下方式编写php来实现上述目标:
if (!function_exists(\'pagination\')){
function pagination($pages = \'\', $range = 2){
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == \'\'){
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages){
$pages = 1;
}
}
if(1 != $pages){
echo \'<ul>\';
//The Below Line of Code Takes effect if there are more than 5 pages of posts and the code renders a single left arrow "<"
//if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo \'<li><a href="\'.get_pagenum_link(1).\'" class="tbutton"><span>«</span></a></li>\';
//The Below Line of Code Takes effect if there are more than 5 pages of posts and the code renders a double left arrow "<<"
//if($paged > 1 && $showitems < $pages) echo \'<li><a href="\'.get_pagenum_link($paged - 1).\'"><span>‹</span></a></li>\';
for ($i=1; $i <= $pages; $i++){
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
echo ($paged == $i)? \'<li><span class="deactive">\'.$i.\'</span></li>\':\'<li><a href="\'.get_pagenum_link($i).\'" class="tbutton">\'.$i.\'</a></li>\';
}
}
//The Below Line of Code Takes effect if there are more than 5 pages of posts and the code renders a single right arrow ">"
//if ($paged < $pages && $showitems < $pages) echo \'<li><a href="\'.get_pagenum_link($paged + 1).\'">›</a></li>\';
//The Below Line of Code Takes effect if there are more than 5 pages of posts and the code can render a double right arrow ">>" with the html encode of "»"
//However instead the code is rendering three dots "..." and the last page number
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo \'<li><li><span class="deactive">...</span></li><a href="\'.get_pagenum_link($pages).\'" class="tbutton">\'.$pages.\'</a></li>\';
echo \'</ul>\';
}
}
}
我还添加了评论。我从
HOW TO BUILD A WORDPRESS POST PAGINATION WITHOUT PLUGIN