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,应包含开始标签

      ,但不需要关闭标签,WordPress会自动关闭这个标签。

      end-callback

      自定义函数

      用来关闭callback中定义的开始标签,而不使用WordPress默认的

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像素大小。

    3, 'style' => 'ol', 'reply_text' => '吐槽', 'avatar_size' => 36, ); wp_list_comments($args); ?>

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

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


    <  id="comment-">
    
        <div id="div-comment-" class="comment-body">
    
    
<?php printf( __( '%s says:' ), get_comment_author_link() ); ?>
comment_approved == '0' ) : ?>
$add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>

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

    $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); ?>

扩展阅读

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

相关函数:

原创文章,作者:,如若转载,请注明出处:https://ce.771633.xyz/1500.html

Like (0)
Previous 2025年3月8日
Next 2025年3月8日

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注