建立一个显示所有评论的留言板 III

2008年07月28日  | 分类: WordPress, 站长推荐  | 标记: , ,
本文涉及的文件:
all-comments.php — 页面模板(请根据自己使用的主题编写)
all-comment-template.php — 评论模板
all-comment-function.php — 分页函数
all-comment-navi.css — 分页条样式

前2篇文章已经讲过,如何利用现有的评论模板来建立留言板。今天我们将给这个留言板加入分页功能。

没有看过的朋友可以回顾
建立一个显示所有评论的留言板 I
建立一个显示所有评论的留言板 II

1. 先来了解下分页的基本原理。
建立一个显示所有评论的留言板 II中用到的SQL,取出了数据库中所有的评论,如果可以根据我们的要求,每次只取出我们需要的部分,就可以实现分页了。
如果想从Mysql数据库中取出连续的部分数据,可以使用“limit”参数,看下面的例子:

select * from table limit 30, 10;
这句SQL的意思是:从table表中取出第31条~第40条数据。
我们只需要定义数据的起点和数据长度,就能得到我们需要的数据段。

2. 分页的实现
Q: 如何得到当前的页码?
A: 当我们点击导航条的时候,页码通过?page=2这样的参数传递回页面,在PHP中通过$_GET['page']来获得。
Q: 如何计算数据的起点?
A: limit的第一个参数是数据的起点,通过公式“$offset = 每页显示的数量 * (当前页码 - 1)”来计算。

有了这样的知识,就可以开工了。
打开all-comment-template.php,在开头加入

// 每页显示的数量
$num_per_page = 20;
// 当前页码
$page_now = $_GET['page'];
// 获取总页数
$query = "select count(*) from $wpdb->posts AS posts, $wpdb->comments AS comments
where posts.ID = comments.comment_post_ID
and posts.post_status = 'publish'
and comments.comment_approved = '1'
and comments.comment_type = ''";
$count = $wpdb->get_var($query);
$max_page = ceil($count / $num_per_page);
// 计算offset
$offset = $num_per_page * ($page_now - 1);

修改建立一个显示所有评论的留言板 II中的SQL,加入offset参数

// 获取点评
$query = "select posts.*, comments.*
from $wpdb->posts as posts, $wpdb->comments as comments
where posts.ID = comments.comment_post_ID
and posts.post_status = 'publish'
and comments.comment_approved = '1'
and comments.comment_type = ''
order by comment_date desc
limit $offset, $num_per_page";

然后,我们可以输出导航条

for($i=1; $i<=$max_page; $i++) {
echo '<span style="width:20xp;"><a href="?page=' . $i . '">' . $i . '</a></span>';
}

这样差不多就大功告成了
当然,你可以对导航条进行一点点修饰,使他更符合我们的使用习惯。

完整的代码差不多应该是这样:

<?php
global $wpdb, $comment, $wp_query;
$num_per_page = 20; // 每页显示
$page_now = $_GET['page'];
 
// 参数验证
if (!isset($num_per_page) || !is_numeric($num_per_page) || $num_per_page < 1) {
    $num_per_page = 1;
}
if (!isset($page_now) || !is_numeric($page_now) || $page_now < 1) {
    $page_now = 1;
}
 
// 获取总页数
$query = "select count(*) from $wpdb->posts AS posts, $wpdb->comments AS comments 
          where posts.ID = comments.comment_post_ID 
            and posts.post_status = 'publish' 
            and comments.comment_approved = '1' 
            and comments.comment_type = ''";
$wp_query->comment_count = $count = $wpdb->get_var($query);
$max_page = ceil($count / $num_per_page);
 
if ($page_now > $max_page) {
    $page_now = 1;
}
 
// 获取offset
$offset = $num_per_page * ($page_now - 1);
 
// 获取点评
$query = "select posts.*, comments.*
          from $wpdb->posts as posts, $wpdb->comments as comments 
          where posts.ID = comments.comment_post_ID 
                and posts.post_status = 'publish' 
                and comments.comment_approved = '1' 
                and comments.comment_type = ''
          order by comment_date desc 
          limit $offset, $num_per_page";
 
$comments = $wpdb->get_results($wpdb->prepare($query));
 
// 输出分页
for($i=1; $i<=$max_page; $i++) {
    echo '<span style="width:20px;"><a href="?page=' . $i . '">' . $i . '</a></span>';
}
// 如果你愿意,可以美化分页信息
// 使用下面的语句输出一个类似postnavi的分页条
// 所需的文件在本文开头已经给出
// $css_url = get_bloginfo("template_url") . "/all-comment-navi.css";
// echo "\n" . '<link rel="stylesheet" href="' . $css_url . '" type="text/css" media="screen" />';
// $function_url =  "all-comment-function.php";
// require($function_url);
// echo get_ecnavi($max_page, $page_now, 5, $count);
 
// 调用显示comments
define('COMMENTS_TEMPLATE', true);
$include = apply_filters('comments_template', TEMPLATEPATH . '/comments.php' );
if (file_exists( $include ) )
	require( $include );
 
else
	require( WP_CONTENT_DIR . '/themes/default/comments.php');
?>
  1. 07月 29th, 2008 @ 16:05
    #1

    没什么实用价值的说

  2. 07月 30th, 2008 @ 10:37
    #2

    我继续晕过去….

  3. 07月 31st, 2008 @ 13:01
    #3

    这个蛮有意思的,收藏了。
    建议直接做成插件^_^

  4. 07月 31st, 2008 @ 13:36
    #4

    stephen:

    这个蛮有意思的,收藏了。
    建议直接做成插件^_^

    正在学习WP,慢慢的会考虑做成插件 :wink:

  5. 07月 31st, 2008 @ 15:07
    #5

    如此通过限定读取comment数量来实现分页的效果,或者应该说分页输出comment。你有没有考虑过,只是简单地通过get page来获取page页面,这样生成的永久链接(?page=)会不会和WP自带的rewrite有冲突?或者文中代码不够完整?
    另外,update_comment_cache($comments);这句话可以去掉,这行代码在没有开启cache的环境下无用的。以及在计算总页数的时候,就可以将总的comments数给同时计算了,没有必要在用第二次查询中计算。而且每点击一个页面,还需要再次读取数据库,这不是浪费么?
    总之,分页的idea不错。建议整理下,发给论坛上,同时pm我下。

  6. 07月 31st, 2008 @ 16:28
    #6

    Thinkagain:

    如此通过限定读取comment数量来实现分页的效果,或者应该说分页输出comment。你有没有考虑过,只是简单地通过get page来获取page页面,这样生成的永久链接(?page=)会不会和WP自带的rewrite有冲突?或者文中代码不够完整?
    另外,update_comment_cache($comments);这句话可以去掉,这行代码在没有开启cache的环境下无用的。以及在计算总页数的时候,就可以将总的comments数给同时计算了,没有必要在用第二次查询中计算。而且每点击一个页面,还需要再次读取数据库,这不是浪费么?
    总之,分页的idea不错。建议整理下,发给论坛上,同时pm我下。

    $comments_count是多余了一次查询,谢谢提醒。
    最近没什么时间去更新这篇文章,我会慢慢整理下的 :smile: 等搞出一个自己比较满意的版本之后再放到论坛

  7. 10月 16th, 2008 @ 12:32
    #7

    没有隐藏留言的功能不爽

  8. 10月 16th, 2008 @ 13:17
    #8

    午夜客 :

    没有隐藏留言的功能不爽

    隐藏留言什么意思?

  9. 10月 30th, 2008 @ 16:29
    #9

    隐藏留言的意思就是我有悄悄话要告诉站长。例如网站有漏洞呀,或是有什么不能让第三个人知道的事情要告诉站长的!留言只有站长看的到,其它人看不到!

  10. 12月 3rd, 2008 @ 15:23
    #10

    请问能否在这个基础上,对wp的归档页来个分页??

    我是指 用wp建的cms,数剧太多的情况下对它分页.

    比如我的小站,数剧就太多了…..

    归档页太长了

TOP