可能有人觉得单纯的分页显得很单调,正好今天有网友问我Typecho如何实现类似我这个文章分页的统计信息的问题。那我就把方法与大家分享一下吧。
这个不是主题实现的,而是要修改原有代码的,首先打开位于/var/Typecho/Widget/Helper/PageNavigator/目录下的Box.php文件。定位到大概34行render函数,我改的如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | /** * 输出盒装样式分页栏 * * @access public * @param string $prevWord 上一页文字 * @param string $nextWord 下一页文字 * @param int $splitPage 分割范围 * @param string $splitWord 分割字符 * @return void */ public function render( $prevWord = 'PREV', $nextWord = 'NEXT', $splitPage = 3, $splitWord = '...' ) { if ($this->_total < 1) { return; } $from = max(1, $this->_currentPage - $splitPage); $to = min($this->_totalPage, $this->_currentPage + $splitPage); // 输出页面信息 echo '<li class="page-info"><span>' . $this->_total . '</span><span>' . $this->_currentPage . '/' . $this->_totalPage . '</span></li>'; //输出上一页 if ($this->_currentPage > 1) { echo '<li><a class="prev" href="' . str_replace( $this->_pageHolder, $this->_currentPage - 1, $this->_pageTemplate) . $this->_anchor . '">' . $prevWord . '</a></li>'; } //输出第一页 if ($from > 1) { echo '<li><a href="' . str_replace($this->_pageHolder, 1, $this->_pageTemplate) . $this->_anchor . '">1</a></li>'; if ($from > 2) { //输出省略号 echo '<li>' . $splitWord . '</li>'; } } //输出中间页 for ($i = $from; $i <= $to; $i ++) { echo '<li><a' . ( $i != $this->_currentPage ? '' : ' class="current"' ) . ' href="' . str_replace( $this->_pageHolder, $i, $this->_pageTemplate ) . $this->_anchor . '">' . $i . '</a></li>'; } //输出最后页 if ($to < $this->_totalPage) { if ($to < $this->_totalPage - 1) { echo '<li>' . $splitWord . '</li>'; } echo '<li><a href="' . str_replace( $this->_pageHolder, $this->_totalPage, $this->_pageTemplate ) . $this->_anchor . '">' . $this->_totalPage . '</a></li>'; } //输出下一页 if ($this->_currentPage < $this->_totalPage) { echo '<li><a class="next" href="' . str_replace( $this->_pageHolder, $this->_currentPage + 1, $this->_pageTemplate) . $this->_anchor . '">' . $nextWord . '</a></li>'; } } |
很明显可以找到下面这一句:
1 2 3 4 | echo '<li class="page-info"><span>' . $this->_total . '</span><span>' . $this->_currentPage . '/' . $this->_totalPage . '</span></li>'; |
在这段函数中$this->_total表示总的记录条数,$this->_currentPage表示当前页码,$this->_totalPage表示总的页码数,怎么样是不是很简单。
typecho好久没有更新了。这样改动源代码,不便于升级啊。
@loosky
是啊,貌似官方主页都打不开了,真担心这个项目会荒废下去,现在Typecho还是有很多Bug有待修复的。在官方源代码里,貌似分页机制是硬编码的,所以只能改源代码了,希望分页这里能够再灵活一些,要不下次更新的话又要改源代码,确实麻烦。
在var\Typecho \Widget\Helper \PageNavigator.php好像这段代码,是不是有什么方法可以在模板直接输出呢….
@蚂蚱
这里实际调用的是/var/Typecho/Widget/Helper/PageNavigator/Box.php 的render方法进行输出的,所以改动了该处,不过抱歉的是模板直接输出我还没找到办法,感觉官方的这个分页不是挺灵活的。