【函數介紹】

wp_list_comments() 是用於讀取 WordPress 文章或者頁面評論數據的函數。

【函數使用】

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

$args
(array) (可選) 函數參數條件選項.
默認:

<?php $args = array(
	'walker'            => null,
	'max_depth'         => '',
	'style'             => 'ul',
	'callback'          => null,
	'end-callback'      => null,
	'type'              => 'all',
	'reply_text'        => 'Reply',
	'page'              => '',
	'per_page'          => '',
	'avatar_size'       => 32,
	'reverse_top_level' => null,
	'reverse_children'  => ''
); ?>

max_depth, per_page, 以及 reverse_top_level 可以再後台 「設置」=》 「討論」 處設置。
per_page:每頁顯示數量
max_depth:最多嵌套
reverse_top_level:是否顯示最新評論

$comments
(array) (可選) 通過 get_comments 查詢得到的評論數據
默認: 返回 get_comments 函數的返回值.

【參數説明】

$avatar_size
(integer) (可選) 頭像大小. 如果通過 http://gravatar.com/ 獲取的頭像尺寸再 1 到 512 像素.
默認: 32
$style
(string) (可選) 評論樣式控制,你可以添加 『div』, 『ol』, 或者』ul』 來展示你的評論,如:

<div ><?php wp_list_comments(array('style' => 'div')); ?></div>

or

<ol ><?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 、列出當前文章或者頁面的評論數據:

<ol >
<?php wp_list_comments(); ?>
</ol>

2 、使用回調函數來自定義評論的展示方式:

<ul >
<?php wp_list_comments('type=comment&callback=mytheme_comment'); ?>
</ul>

回調函數為 mytheme_comment,你可以添加在你主題的 functions.php 文件中,函數方法如下:

//author by WordPress 教程網 (wpnoob.cn)
function mytheme_comment($comment, $args, $depth) {
		$GLOBALS['comment'] = $comment;
		extract($args, EXTR_SKIP);

		if ( 'div' == $args['style'] ) {
			$tag = 'div';
			$add_below = 'comment';
		} else {
			$tag = 'li';
			$add_below = 'div-comment';
		}
?>
		<<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
		<?php if ( 'div' != $args['style'] ) : ?>
		<div id="div-comment-<?php comment_ID() ?>" >
		<?php endif; ?>
		<div >
		<?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
		<?php printf(__('<cite >%s</cite> <span >says:</span>'), get_comment_author_link()) ?>
		</div>
<?php if ($comment->comment_approved == '0') : ?>
		<em ><?php _e('Your comment is awaiting moderation.') ?></em>
		<br />
<?php endif; ?>

		<div ><a href="<?php%20echo%20htmlspecialchars(%20get_comment_link(%20$comment->comment_ID%20)%20)%20?>">
			<?php
				/* translators: 1: date, 2: time */
				printf( __('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','' );
			?>
		</div>


		<?php comment_text() ?>

		<div >
		<?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
		</div>
		<?php if ( 'div' != $args['style'] ) : ?>
		</div>
		<?php endif; ?>
<?php
        }

3 、將評論顯示在文章或者頁面中:

//author by WordPress 教程網 (wpnoob.cn)
<ol >
	<?php
		//Gather comments for a specific page/post
		$comments = get_comments(array(
			'post_id' => XXX,
			'status' => 'approve' //Change this to the type of comments to be displayed
		));

		//Display the list of comments
		wp_list_comments(array(
			'per_page' => 10, //Allow comment pagination
			'reverse_top_level' => false //Show the latest comments at the top of the list
		), $comments);
	?>
</ol>

【源代碼】

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