您的位置:首页»WordPress 教程, 进阶使用»  WordPress2.7 评论分页完全使用教程

WordPress2.7 评论分页完全使用教程

大家知道,评论数太多会减慢页面载入速度,将评论分页显示有助于减少数据库查询次数,从而加快页面载入速度。所幸的是,WordPress2.7 对 WordPress 评论系统进行了一些列增强,不但可以开启嵌套评论和直接在后台回复评论,还支持两种评论分页样式。只要你的主题使用了 wp_list_comments 这个函数,我们即可在 comments.php 中任选以下代码,即可实现评论分页功能,本篇 WordPress 教程,将详细介绍其使用方法。
第一种分页方式:上一页和下一页式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
// 如果用户在后台选择要显示评论分页
if (get_option('page_comments')) {
// 判断评论是否需要分页
$comment_pages = paginate_comments_links('echo=0');
if ($comment_pages) {
?>
<div id="commentnavi">
<span class="floatleft"><?php previous_comments_link(__('&laquo; Older Comments')) ?></span>
<span class="floatright"><?php next_comments_link(__('Newer Comments &raquo;')) ?></span>
</div>
<?php
   }
 }
 ?>

第二种分页方式:数字导航式分页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
	// 如果用户在后台选择要显示评论分页
	if (get_option('page_comments')) {
		// 判断评论是否需要分页
		$comment_pages = paginate_comments_links('echo=0');
		if ($comment_pages) {
?>
	<div id="commentnavi">
		<?php echo $comment_pages; ?>
	</div>
<?php
		}
	}
?>

但是,WordPress2.7 的评论分页功能还有两个 bug:

  1. 以上两种分页方式只适用于使用默认和以“/”结尾的永久链接结构
  2. 如果我们使用日志的 ID 号或伪静态这两种永久链接结构之一,那么,我们的评论分页将会出现问题:

    http://oursite/archives/111comment-page-1#comments

    点击这个链接试试,404。当我们试图在日志 id 号 111 后面加上一个反斜线,问题便解决了:

    http://oursite/archives/111/comment-page-1#comments

    点击这个链接,便正确跳转到评论第一页。聪明的你可能已经知道原因了吧?那就是 paginate_comments_links 函数自动去除了 “/”,那么,既然号对了脉,我们对症下药即是,打开 wp-includes 目录下的 link-template.php 这个文件,将 paginate_comments_links 默认设置:

    1
    2
    
    $defaults['base'] = user_trailingslashit(get_permalink() . 'comment-page-%#%', 
    'commentpaged');

    修改为

    1
    2
    
    $defaults['base'] = trailingslashit(get_permalink()) . user_trailingslashit('comment-page-
    %#%', 'commentpaged');

    这样,WordPress 便会自动在链接地址中加上 “/” 这个符号,我们也就能正常访问分页评论了。

  3. 评论分页会致使文章内容重复,不利于 SEO
  4. 当我们使用评论分页时,便会出现类似以下结构的链接地址:

    http://oursite/postname/comment-page-1/#comments

    http://oursite/postname/comment-page-2/#comments

    http://oursite/postname/comment-page-3/#comments

    不难发现,点击这些链接地址都会出现相同的文章内容。为了避免分页评论里出现重复内容,我们只输出文章标题和链接即可,打开你所使用主题的 functions.php 文件,加入以下代码即可:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
     function seo_paged_comments_content_filter($t = '') {
    	$cpage = intval(get_query_var('cpage'));
    	if ( ! empty( $cpage ) ) {
    		remove_filter('the_content', 'seo_paged_comments_content_filter');
    		$t = sprintf('<p>Part of comments at ');
    		$t .= sprintf('<a href="%1$s">%2$s</a></p>', get_permalink(), get_the_title());
    	}
    	return $t;
    }
    add_filter('the_content', 'seo_paged_comments_content_filter');

如果大家在使用 WordPress 的过程中还有其他问题,请留言或加入站趣-分享建站的乐趣 QQ群:8329980 讨论。

6个评论

  • Reply 1 aunsen

    January 14th, 2009 at 13:08

    沙发!
    这个好,刚刚改好!

    • Reply 2 辐射鱼

      January 14th, 2009 at 14:05

      解决了就好,你和火星人改那主题不错,感觉的你的配色要更好些

  • Reply 3 testman

    January 14th, 2009 at 14:17

    劳资拿走一块地毯

  • Reply 4 林晨

    January 14th, 2009 at 14:37

    博客整体感觉不错

  • Reply 5 dayan

    August 8th, 2009 at 16:49

    不错