自定义最新帖子分页

时间:2014-10-18 作者:Amen Ra

我成功地实现了“最新帖子”页面的分页。但是我想在当前页之后分隔数字,并在它们和最后一页之间放置分隔符。下面是我要实现的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>&laquo;</span></a></li>\';
            if($paged > 1 && $showitems < $pages) echo \'<li><a href="\'.get_pagenum_link($paged - 1).\'"><span>&lsaquo;</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).\'">&rsaquo;</a></li>\';  
             if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo \'<li><a href="\'.get_pagenum_link($pages).\'">&raquo;</a></li>\';
             echo "</ul></div>\\n";
        }
    }
}
我如何编辑这个php以使其呈现上面显示的html?

1 个回复
最合适的回答,由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>&laquo;</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>&lsaquo;</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).\'">&rsaquo;</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 "&raquo;"
             //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

结束

相关推荐

foreach pagination

我正在寻求有关如何对foreach输出分页的帮助。我查看了其他问题和答案,但找不到适合我的解决方案,也找不到可以自己解决的解决方案。现在,下面的代码将所有内容输出到表行中。当然,我的问题是,它将所有数据转储到一个页面上——因此我需要分页。我想为页面上的每11项分页。这一页是一份杂志档案,每年有11期——因此每一页相当于我们杂志的1年。第一页应该是第1-11期,第二页应该是第12-22期,等等。我们有10年的杂志期。任何帮助都将不胜感激。非常感谢。<table> <tr>&#