您现在的位置是:首页 > 网站制作 > WordpressWordpress

wp_list_comments()使用回调函数自定义评论展示方式

杰帅2023-06-16【Wordpress】人已围观

简介wp_list_comments()是用于读取wordpress文章或者页面评论数据的函数,把 WordPress 的评论功能很好的进行了模块化,wp_list_comments 函数在主题中配合comments_template 函数联合使用可以很好的将 WordPress 的评论功能独立出来,而且可以更好对评论嵌套层数、每页显示评论数量、评论样式等进行控制。

wp_list_comments()是用于读取wordpress文章或者页面评论数据的函数,把 WordPress 的评论功能很好的进行了模块化,wp_list_comments 函数在主题中配合comments_template 函数联合使用可以很好的将 WordPress 的评论功能独立出来,而且可以更好对评论嵌套层数、每页显示评论数量、评论样式等进行控制。

1、用法

<?php  wp_list_comments( $args ); ?>

2、参数使用

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

3、参数说明

(1)$walker 自定义样式类名
(2)$avatar_size (可选) 头像大小 默认: 32
(3)$style (可选)评论样式控制,你可以添加 ‘div’, ‘ol’, 或者’ul’, 来展示你的评论,如:

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

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

默认值是’ul’

(4)$type (可选) 评论展现方式,参数可以是 ‘all’、’comment’、’trackback’、’pingback’、’pings’. ‘pings’ 包括’trackback’ 和 ‘pingback’.
默认值: ‘all’
(5)$reply_text (可选) 评论的回复链接,(可以通过函数get_comment_reply_link function 获取)
默认: ‘Reply’
(6)$login_text (可选)用户必须登录后评论的提示信息,默认: ‘Log in to Reply’
(7)$callback (可选) 回调函数,通过回调函数来自定义你的评论展示方式。Default: null
(8)$end-callback (可选) 关闭评论后调用的自定义函数,Default: null
(9)$reverse_top_level (可选)评论数据是否倒序显示,Default: null
(10)$reverse_children (可选) 子评论数据是否倒序显示,Default:null

4、自定义评论列表制作

php wp_list_comments();可以实现评论列表的展示,大多数主题都是使用的默认调用函数,使用这个虽然增加了主题制作的方便性,但是它是调用系统给你定义的id和class,所以对于样式的修改带来麻烦,而且你也不敢保证wordpress的更新不会更换id和class,且自定义性较弱,需要加入一些东西比较麻烦,比如说给评论增加v认证,博主高亮,楼层显示,这无疑就要去修改wordpress系统wp-include下的comment-template.php模版,修改的东西随着更新又会消失。针对这一问题,我们需要替换评论列表的函数,在主题下的comments.php找到

<?php wp_list_comments(); ?>

把这句替换为以下代码即可:

<?php wp_list_comments( array(
    'callback'     =>  'bootstrapwp_comment',
  ) );  ?>

其中bootstrapwp_comment自定义的回调函数。

5、在functions.php添加自定义回调函数bootstrapwp_comment,函数方法如下:

/*
 * 评论列表的显示
 */
if ( ! function_exists( 'bootstrapwp_comment' ) ) :
function bootstrapwp_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case 'pingback' :
		case 'trackback' :
	  // 用不同于其它评论的方式显示 trackbacks 。
	?>
	<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
		<p><?php _e( 'Pingback:', 'bootstrapwp' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'bootstrapwp' ), '<span class="edit-link">', '</span>' ); ?>
		</p>
	<?php
		break;
		default :
		// 开始正常的评论
		global $post;
	 ?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<article id="comment-<?php comment_ID(); ?>" class="media comment">
			<div class="pull-left">
  			<?php // 显示评论作者头像 
  			  echo get_avatar( $comment, 64 ); 
  			?>
			</div>
			<?php // 未审核的评论显示一行提示文字
			  if ( '0' == $comment->comment_approved ) : ?>
  			<p class="comment-awaiting-moderation">
  			  <?php _e( 'Your comment is awaiting moderation.', 'bootstrapwp' ); ?>
  			</p>
			<?php endif; ?>
			<div class="media-body">
				<h4 class="media-heading">
  				<?php // 显示评论作者名称
  				    printf( '%1$s %2$s',
  						get_comment_author_link(),
  						// 如果当前文章的作者也是这个评论的作者,那么会出现一个标签提示。
  						( $comment->user_id === $post->post_author ) ? '<span class="label label-info"> ' . __( 'Post author', 'bootstrapwp' ) . '</span>' : ''
  					);
  				?>
  		    <small>
    				<?php // 显示评论的发布时间
    				    printf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
    						esc_url( get_comment_link( $comment->comment_ID ) ),
    						get_comment_time( 'c' ),
    					  // 翻译: 1: 日期, 2: 时间
    						sprintf( __( '%1$s %2$s', 'fenikso' ), get_comment_date(), get_comment_time() )
    					);
    				?>
  				</small>
				</h4>
				<?php // 显示评论内容
				  comment_text(); 
				?>
				<?php // 显示评论的编辑链接 
				  edit_comment_link( __( 'Edit', 'bootstrapwp' ), '<p class="edit-link">', '</p>' ); 
				?>
				<div class="reply">
					<?php // 显示评论的回复链接 
					  comment_reply_link( array_merge( $args, array( 
					    'reply_text' =>  __( 'Reply', 'bootstrapwp' ), 
					    'after'      =>  ' <span>&darr;</span>', 
					    'depth'      =>  $depth, 
					    'max_depth'  =>  $args['max_depth'] ) ) ); 
					?>
				</div>
			</div>
		</article>
	<?php
		break;
	endswitch; // end comment_type check
}
endif;

6、其他说明

在早期的主题中,wp_list_comments 函数我们不常看到,该函数是从 WordPress 2.7.0 版本后才出现的,相较于之前的获取评论对象,再进行遍历的方法,wp_list_comments 简单的用一个函数做成类似于主循环的效果,做成模块化,对于调用和定制非常方便。
函数官方文档地址(英文):

http://codex.wordpress.org/Function_Reference/wp_list_comments

Tags:wordpress函数   wordpress教程   wp_list_comments()   评论

很赞哦! ()

文章评论

本站推荐

站点信息

  • 建站时间:2018-10-24
  • 网站程序:帝国CMS7.5
  • 主题模板《今夕何夕》
  • 文章统计1172篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们