【函式介紹】

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.