wordpress读取评论数据函数:wp_list_comments()


【函数介绍】

wp_list_comments()是用于读取wordpress文章或者页面评论数据的函数。

【函数使用】

  1. <?php wp_list_comments( $args, $comments ); ?>

$args
(array) (可选)函数参数条件选项.
默认:

  1. <?php $args = array(
  2. 'walker' => null,
  3. 'max_depth' => '',
  4. 'style' => 'ul',
  5. 'callback' => null,
  6. 'end-callback' => null,
  7. 'type' => 'all',
  8. 'reply_text' => 'Reply',
  9. 'page' => '',
  10. 'per_page' => '',
  11. 'avatar_size' => 32,
  12. 'reverse_top_level' => null,
  13. 'reverse_children' => ''
  14. ); ?>

max_depth, per_page, 以及reverse_top_level 可以再后台“设置”=》“讨论”处设置。
per_page:每页显示数量
max_depth:最多嵌套
reverse_top_level:是否显示最新评论
plset

$comments
(array) (可选) 通过get_comments查询得到的评论数据
默认: 返回get_comments函数的返回值.

【参数说明】

$avatar_size
(integer) (可选) 头像大小. 如果通过http://gravatar.com/ 获取的头像尺寸再 1 到 512像素.
默认: 32
$style
(string) (可选) 评论样式控制,你可以添加 ‘div’, ‘ol’, 或者’ul’ 来展示你的评论,如:

  1. <div class="commentlist"><?php wp_list_comments(array('style' => 'div')); ?></div>

or

  1. <ol class="commentlist"><?php wp_list_comments(array('style' => 'ol')); ?></ol>

默认: ‘ul’
$type
(string) (可选) 评论展现方式.可以是 ‘all’, ‘comment’, ‘trackback’, ‘pingback’, or ‘pings’. ‘pings’ 包含了’trackback’和’pingback’
默认: ‘all’
$page
(string) (可选) 当前页数.
默认: null
$reply_text
(string) (可选) 评论的回复链接. (可以通过函数get_comment_reply_link function 获取)
默认: ‘Reply’
$login_text
(string) (可选)用户必须登录后评论的提示信息.
默认: ‘Log in to Reply’
$callback
(string) (可选) 回调函数,通过回调函数来自定义你的评论展示方式。
Default: null
$end-callback
(string) (可选) 关闭评论后调用的自定义函数
Default: null
$reverse_top_level
(boolean) (可选)评论数据是否倒序显示
Default: null
$reverse_children
(boolean) (可选) 子评论数据是否倒序显示。
Default:null

【函数实例】

1、列出当前文章或者页面的评论数据:

  1. <ol class="commentlist">
  2. <?php wp_list_comments(); ?>
  3. </ol>

2、使用回调函数来自定义评论的展示方式:

  1. <ul class="commentlist">
  2. <?php wp_list_comments('type=comment&callback=mytheme_comment'); ?>
  3. </ul>

回调函数为mytheme_comment,你可以添加在你主题的functions.php文件中,函数方法如下:

  1. //author by wordpress教程网(shouce.ren)
  2. function mytheme_comment($comment, $args, $depth) {
  3. $GLOBALS['comment'] = $comment;
  4. extract($args, EXTR_SKIP);
  5.  
  6. if ( 'div' == $args['style'] ) {
  7. $tag = 'div';
  8. $add_below = 'comment';
  9. } else {
  10. $tag = 'li';
  11. $add_below = 'div-comment';
  12. }
  13. ?>
  14. <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
  15. <?php if ( 'div' != $args['style'] ) : ?>
  16. <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
  17. <?php endif; ?>
  18. <div class="comment-author vcard">
  19. <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  20. <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
  21. </div>
  22. <?php if ($comment->comment_approved == '0') : ?>
  23. <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
  24. <br />
  25. <?php endif; ?>
  26.  
  27. <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
  28. <?php
  29. /* translators: 1: date, 2: time */
  30. printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),' ','' );
  31. ?>
  32. </div>
  33.  
  34.  
  35. <?php comment_text() ?>
  36.  
  37. <div class="reply">
  38. <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
  39. </div>
  40. <?php if ( 'div' != $args['style'] ) : ?>
  41. </div>
  42. <?php endif; ?>
  43. <?php
  44. }

3、将评论显示在文章或者页面中:

  1. //author by wordpress教程网(shouce.ren)
  2. <ol class="commentlist">
  3. <?php
  4. //Gather comments for a specific page/post
  5. $comments = get_comments(array(
  6. 'post_id' => XXX,
  7. 'status' => 'approve' //Change this to the type of comments to be displayed
  8. ));
  9.  
  10. //Display the list of comments
  11. wp_list_comments(array(
  12. 'per_page' => 10, //Allow comment pagination
  13. 'reverse_top_level' => false //Show the latest comments at the top of the list
  14. ), $comments);
  15. ?>
  16. </ol>

【源代码】

wp_list_comments() 位于 wp-includes/comment-template.php.