在我们为 WordPress 开发插件的时候,在涉及到列表显示许多记录的时候,肯定要考虑分页显示的问题。自然的,不管是简单性也好,还是统一性,直接参考下 WordPress 自己是怎么实现的,看看 Posts 或 User 管理页面的实现代码,比如 User 管理页面的实现代码在 wp-admin/includes/user.php 中的 WP_User_Search 类。默认实现其实是很好看的,如:。那我们如何在自己的插件里实现这样的效果呢,比如像这个:
我的做法是新建了自己的一个 Pagination 类,My_Pagination 类的内容如下:
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
<?php /** * Used for show paged records * @author Unmi */ class FN_Pagination { /** * data to display */ var $results; /** * Page number. * @var int */ var $current_page_number; /** * Amount of records to display per page. * @access public * @var int */ var $records_per_page = 20; /** * @var int */ var $total_record_count = 1; /** * @access private * @var string */ var $paging_text; /** * @var string */ var $base_page_url; /** * PHP4 Constructor - Sets up the object properties. * @param string $prepare_sql_str 一个不带 LIMIT 的 sql 语句 * @param string $base_page_url 分页跳转时的 URL,用于生成分页的 URL * @param string $current_page_number 当前页号,从请求中可取得 * @param int $records_per_page 每页记录数 * @return FN_Pagination */ function FN_Pagination ($prepare_sql_str,$base_page_url, $current_page_number = '',$records_per_page=20) { $this->base_page_url = $base_page_url; $this->current_page_number = (int) ( '' == $current_page_number ) ? 1 : $current_page_number; $this->records_per_page = $records_per_page; $this->query($prepare_sql_str); $this->do_paging(); } /** * * @access public */ function query($prepare_sql_str) { global $wpdb; $get_count_sql = preg_replace("/SELECT.+?FROM/is","SELECT count('c') FROM",$prepare_sql_str,1); //if sql string contains group by if(preg_match("/group.+?by/is", $get_count_sql) >0 ) { //$this->total_record_count = count($wpdb->get_col($wpdb->prepare($get_count_sql))); $this->total_record_count = $wpdb->get_var( $wpdb->prepare("select count('c') from (($get_count_sql) as a)")); }else{ $this->total_record_count = $wpdb->get_var($wpdb->prepare($get_count_sql)); } //or //$this->total_record_count = $wpdb->get_var( // $wpdb->prepare("select count('c') from (($get_count_sql) as a)")); $start_record = ($this->current_page_number-1) * $this->records_per_page; //此方面前面部分只是为了获得记录总数,分了带不带 group by 的情况 $this->results = $wpdb->get_results($wpdb->prepare($prepare_sql_str. " LIMIT $start_record,$this->records_per_page")); } /** * * @since unknown * @access public */ function do_paging() { if ( $this->total_record_count > $this->records_per_page ) { // have to page the results //调用了 wp-includes/general-template.php 中的 paginate_links() 方法用于产生用于页面显示的导航链接 $this->paging_text = paginate_links( array( 'total' => ceil($this->total_record_count / $this->records_per_page), 'current' => $this->current_page_number, 'base' => $this->base_page_url . '&%_%', 'format' => 'page_no=%#%' ) ); //理解一下用的占位符,通过 format 和 base,会生成像如下的链接,注意何时 ?%_%,何时&%_% // http://unmi/wp-admin/admin.php?page=my_plugin/tracking-logs.php&page_no=2 if ( $this->paging_text ) { $this->paging_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s–%s of %s' ) . '</span>%s', number_format_i18n( ( $this->current_page_number - 1 ) * $this->records_per_page + 1 ), number_format_i18n( min( $this->current_page_number * $this->records_per_page, $this->total_record_count ) ), number_format_i18n( $this->total_record_count ), $this->paging_text ); } } } /** * @access public */ function get_results() { return (array) $this->results; } /** * Displaying paging text. * @access public */ function page_links() { echo $this->paging_text; } /** * Whether paging is enabled. * @see do_paging() Builds paging text. * @access public * @return bool */ function results_are_paged() { if ( $this->paging_text ) return true; return false; } } ?> |
使用时,当然先要引入上面那个文件了,然后通过下面的代码取得当页要显示的记录集,同时也得到了你要的分页连接:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php @$page_no = $_GET['page_no']; $sql = "你的 sql 语句,可以复杂点的"; $pagination = new FN_Pagination($sql,'admin.php?page=my_plugin/tracking-logs.php',$page_no); $records = $pagination->get_results(); //然后列表循环显示你的 $records 记录 ?> <?php /** 然后在你想要放置翻页链接的地方加上下面的代码,注意 <div> 层次,以运用正确的样式**/?> <div class="tablenav"> <?php if ( $pagination->results_are_paged() ) : ?> <div class="tablenav-pages"><?php $pagination->page_links(); ?></div> <?php endif; ?> </div> |
就这样,整个过程就完成了,看看页面是不是你想要的结果。上面的分页类暂时未加入条件搜索和排序的支持,以后还得进一步扩充的。
本文链接 https://yanbin.blog/admin-pagination/, 来自 隔叶黄莺 Yanbin Blog
[版权声明] 本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。
Test....