WordPress评论列表标签:wp_list_comments

WordPress模板标签wp_list_comments用于输出评论列表,需要用在评论模板comments.php中。

wp_list_comments( array|string $args = '', array $comments = null )

函数参数

$args

数组或字符串值,可选

wp_list_comments()函数$args参数默认的值如下:

$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'  => '',
	'format'            => 'html5',
	'short_ping'        => false,
	'echo'              => true
);

$comments

数组,可选,默认值:get_comments()函数返回的值

参数可用的值

wp_list_comments()函数$args参数可用的值如下:

walker

对象,默认值:null

提供一个自定义的类用于输出自己的评论列表HTML,参考Walker_Comment()

max_depth

整数型,默认为空

评论嵌套多少层,如果不传递任何值,即默认的空值,那么将使用后台讨论设置中的嵌套层数设置。

  • 空:使用后台讨论设置中的嵌套层数设置,如果该设置不存在,则为-1;
  • -1:无限嵌套

style

字符串值,默认值:ul

使用何种标签输出评论列表,可选的值有:div、ol、ul

callback

自定义函数

使用一个自定义函数来输出评论列表,可以自定义输出评论的HTML,应包含开始标签<div>、<ol>或<ul>,但不需要关闭标签,WordPress会自动关闭这个标签。

end-callback

自定义函数

用来关闭callback中定义的开始标签,而不使用WordPress默认的</div>、</ol>或</ul>。

type

字符串值,默认值:all

评论的类型,可选值有:all、comment、trackback、pingback、pings(等于trackback和pingback)

reply_text

字符串值,默认值:Reply

评论回复链接的锚文本

page

整数型,默认为空

当前分页的页码

per_page

整数型,默认为空

每个页面显示的评论数量,默认取后台讨论设置每页显示评论数量的值。

avatar_size

整数型,默认值:32

用户头像的大小

reverse_top_level

布尔值,默认值:null

如果为true,等效于讨论设置中在每个页面顶部显示“新的”评论,如果为false,等效于“旧的”评论。

reverse_children

布尔值,默认为空

如果设置为真,将显示最新的子评论,但我在测试中没发现有任何作用。

format

字符串值,默认值:html5

以何种HTML版本风格输出评论列表

  • html5
  • xhtml

short_ping

布尔值,默认值:false

是否使用short ping

echo

布尔值,默认值:true

是否输出结果,如果为false,只返回结果而不是输出。

函数使用示例

以ol列表输出评论,嵌套3层,回复评论链接的锚文本修改为“吐槽”,用户头像设置为36像素大小。

<ol>
<?php
	$args = array(
		'max_depth' => 3,
		'style' => 'ol',
		'reply_text' => '吐槽',
		'avatar_size' => 36,
	);
	wp_list_comments($args);
?>
</ol>

下面的代码输出一样的结果:

<ol>
	<?php wp_list_comments('max_depth=3&style=ol&reply_text=吐槽&avatar_size=36'); ?>
</ol>

使用callback的示例,我们将在mytheme_comment()函数中定义评论列表的输出格式。

<?php
function mytheme_comment($comment, $args, $depth) {
    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() ?>" class="comment-body">
    <?php endif; ?>
    <div class="comment-author vcard">
        <?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
        <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?>
    </div>
    <?php if ( $comment->comment_approved == '0' ) : ?>
         <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em>
          <br />
    <?php endif; ?>

    <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>">
        <?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 class="reply">
        <?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 } ?>
<ul class="commentlist">
	<?php wp_list_comments( 'type=comment&callback=mytheme_comment' ); ?>
</ul>

输出指定文章或页面评论的例子:

<ol class="commentlist">
	<?php
		//Gather comments for a specific page/post 
		$comments = get_comments(array(
			'post_id' => $id,
			'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 oldest comments at the top of the list
		), $comments);
	?>
</ol>

扩展阅读

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

相关函数:

阿里云