WordPress函数wp_list_comments()显示帖子评论列表

WordPress函数wp_list_comments()显示帖子评论列表

WordPress函数wp_list_comments()显示帖子评论列表

  在 WordPress CMS内容管理系统中,wp_list_comments()是一个内置函数,用于列出特定帖子的评论。它接受许多可用于自定义输出的参数,例如列表的样式、注释的格式以及是否显示线程注释。

  推荐:SeedPro Comming Soon Pro插件WordPress即将推出页面插件

wp_list_comments()函数基本语法

<ul class="commentlist">
	<?php 
	wp_list_comments( [ 
		'type'     => 'comment',
		'callback' => 'mytheme_comment',
	] );
	?>
</ul>

描述

  显示当前帖子的永久链接

用法

wp_list_comments( $args, $comments );

参数

  $args

  (string|array) ( 可选) 帖子 ID 或帖子对象。默认是全局的$post

  $comments

   (WP_Comment[]) – 可选。WP_Comment 对象数组。

更多默认参数

$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'  => '',
	'format'            => 'html5', // or 'xhtml' if no 'HTML5' theme support
	'short_ping'        => false,   // @since 3.6
	'echo'              => true     // boolean, default is true
);

  推荐:8个最佳Chrome ChatGPT扩展程序

wp_list_comments()函数

  wp_list_comments()功能还可用于显示线程评论。线程评论允许用户回复其他评论,回复缩进以显示对话树。要显示线程评论,您需要将thread_comments参数设置为true(源文件可参考这里

function wp_list_comments( $args = array(), $comments = null ) {
	global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;

	$in_comment_loop = true;

	$comment_alt        = 0;
	$comment_thread_alt = 0;
	$comment_depth      = 1;

	$defaults = 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'  => '',
		'format'            => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
		'short_ping'        => false,
		'echo'              => true,
	);

	$parsed_args = wp_parse_args( $args, $defaults );

	/**
	 * Filters the arguments used in retrieving the comment list.
	 *
	 * @since 4.0.0
	 *
	 * @see wp_list_comments()
	 *
	 * @param array $parsed_args An array of arguments for displaying comments.
	 */
	$parsed_args = apply_filters( 'wp_list_comments_args', $parsed_args );

	// Figure out what comments we'll be looping through ($_comments).
	if ( null !== $comments ) {
		$comments = (array) $comments;
		if ( empty( $comments ) ) {
			return;
		}
		if ( 'all' !== $parsed_args['type'] ) {
			$comments_by_type = separate_comments( $comments );
			if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) {
				return;
			}
			$_comments = $comments_by_type[ $parsed_args['type'] ];
		} else {
			$_comments = $comments;
		}
	} else {
		/*
		 * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
		 * perform a separate comment query and allow Walker_Comment to paginate.
		 */
		if ( $parsed_args['page'] || $parsed_args['per_page'] ) {
			$current_cpage = get_query_var( 'cpage' );
			if ( ! $current_cpage ) {
				$current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
			}

			$current_per_page = get_query_var( 'comments_per_page' );
			if ( $parsed_args['page'] != $current_cpage || $parsed_args['per_page'] != $current_per_page ) {
				$comment_args = array(
					'post_id' => get_the_ID(),
					'orderby' => 'comment_date_gmt',
					'order'   => 'ASC',
					'status'  => 'approve',
				);

				if ( is_user_logged_in() ) {
					$comment_args['include_unapproved'] = array( get_current_user_id() );
				} else {
					$unapproved_email = wp_get_unapproved_comment_author_email();

					if ( $unapproved_email ) {
						$comment_args['include_unapproved'] = array( $unapproved_email );
					}
				}

				$comments = get_comments( $comment_args );

				if ( 'all' !== $parsed_args['type'] ) {
					$comments_by_type = separate_comments( $comments );
					if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) {
						return;
					}

					$_comments = $comments_by_type[ $parsed_args['type'] ];
				} else {
					$_comments = $comments;
				}
			}

			// Otherwise, fall back on the comments from `$wp_query->comments`.
		} else {
			if ( empty( $wp_query->comments ) ) {
				return;
			}
			if ( 'all' !== $parsed_args['type'] ) {
				if ( empty( $wp_query->comments_by_type ) ) {
					$wp_query->comments_by_type = separate_comments( $wp_query->comments );
				}
				if ( empty( $wp_query->comments_by_type[ $parsed_args['type'] ] ) ) {
					return;
				}
				$_comments = $wp_query->comments_by_type[ $parsed_args['type'] ];
			} else {
				$_comments = $wp_query->comments;
			}

			if ( $wp_query->max_num_comment_pages ) {
				$default_comments_page = get_option( 'default_comments_page' );
				$cpage                 = get_query_var( 'cpage' );
				if ( 'newest' === $default_comments_page ) {
					$parsed_args['cpage'] = $cpage;

					/*
					* When first page shows oldest comments, post permalink is the same as
					* the comment permalink.
					*/
				} elseif ( 1 == $cpage ) {
					$parsed_args['cpage'] = '';
				} else {
					$parsed_args['cpage'] = $cpage;
				}

				$parsed_args['page']     = 0;
				$parsed_args['per_page'] = 0;
			}
		}
	}

	if ( '' === $parsed_args['per_page'] && get_option( 'page_comments' ) ) {
		$parsed_args['per_page'] = get_query_var( 'comments_per_page' );
	}

	if ( empty( $parsed_args['per_page'] ) ) {
		$parsed_args['per_page'] = 0;
		$parsed_args['page']     = 0;
	}

	if ( '' === $parsed_args['max_depth'] ) {
		if ( get_option( 'thread_comments' ) ) {
			$parsed_args['max_depth'] = get_option( 'thread_comments_depth' );
		} else {
			$parsed_args['max_depth'] = -1;
		}
	}

	if ( '' === $parsed_args['page'] ) {
		if ( empty( $overridden_cpage ) ) {
			$parsed_args['page'] = get_query_var( 'cpage' );
		} else {
			$threaded            = ( -1 != $parsed_args['max_depth'] );
			$parsed_args['page'] = ( 'newest' === get_option( 'default_comments_page' ) ) ? get_comment_pages_count( $_comments, $parsed_args['per_page'], $threaded ) : 1;
			set_query_var( 'cpage', $parsed_args['page'] );
		}
	}
	// Validation check.
	$parsed_args['page'] = (int) $parsed_args['page'];
	if ( 0 == $parsed_args['page'] && 0 != $parsed_args['per_page'] ) {
		$parsed_args['page'] = 1;
	}

	if ( null === $parsed_args['reverse_top_level'] ) {
		$parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) );
	}

	wp_queue_comments_for_comment_meta_lazyload( $_comments );

	if ( empty( $parsed_args['walker'] ) ) {
		$walker = new Walker_Comment();
	} else {
		$walker = $parsed_args['walker'];
	}

	$output = $walker->paged_walk( $_comments, $parsed_args['max_depth'], $parsed_args['page'], $parsed_args['per_page'], $parsed_args );

	$in_comment_loop = false;

	if ( $parsed_args['echo'] ) {
		echo $output;
	} else {
		return $output;
	}
}

  推荐:NEX-Forms插件下载WordPress表单生成器插件+ Addons

如何使用wp_list_comments()

  默认使用,显示评论列表。它用于 comments.php 模板文件。评论中树视图和分页的存在是通过管理面板Settings > Discussion控制的

$cpage = get_query_var ( 'cpage' ) ? get_query_var ( 'cpage' ) : 1 ;

wp_list_comments (
	[
		'avatar_size'        => 60 ,
		 'short_ping'         => true ,
		 'type'               => 'comment' ,
		 'callback'           => 'ic_comment_list' ,
		 'per_page'           => get_option ( 'comments_per_page' ),
		 'page'               => $ cpage ,
		 'reverse_top_level' => get_option ( 'default_comments_page' ) === 'oldest' ? 假的:
	]
);

  显示特定帖子的评论。

<ol class="commentlist">
	<?php
	// Retrieve post comments with ID XXX from the database 
	$comments = get_comments( [
		'post_id' => XXX,
		'status' => 'approve' // moderated comments
	] );

	// generate a list of received comments
	wp_list_comments( [
		'per_page'          => 10,   // Pagination of comments - 10 per page
		'reverse_top_level' => false // Show last comments at the beginning
	], $comments );
	?>
</ol>

  自定义复选框字段,创建用于添加评论的自定义表单。它包括一个用户同意保存他们的姓名、电子邮件和网站的复选框。

$comment_form = array(
'fields' => array(
'cookies' => '<p class="comment-form-cookies-consent">
<input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' />
<label for="wp-comment-cookies-consent">' . __( 'Save my name, email, and website in this browser for the next time I comment.' ) . '</label>
</p>',
),
);
comment_form( $comment_form );

  使用自定义评论显示仅显示评论,仅显示评论(不包括 pingbackstrackbacks),使用自定义回调函数mytheme_comment来控制每个评论的显示方式

<ul class="commentlist">
wp_list_comments( 'type=comment&callback=mytheme_comment' );
</ul>

  自定义评论显示回调函数,此回调函数mytheme_comment是一种自定义方式,用于控制每条评论的显示方式。它包括基于评论是否有子评论的条件检查和格式设置。请注意,函数末尾不包含结束标记,因为 WordPress 会在列出任何子元素后添加适当的结束标记

function mytheme_comment($comment, $args, $depth) {
if ( 'div' === $args['style'] ) {
$tag = 'div';
$add_below = 'comment';
} else {
$tag = 'li';
$add_below = 'div-comment';
}
echo '<',$tag,' ',comment_class(empty($args['has_children']) ? '' : 'parent'),' id="comment-',comment_ID(),'">';
if ( 'div' != $args['style'] ) {
echo '<div id="div-comment-',comment_ID(),'" class="comment-body">';
}
// Rest of the function...
}

  颠倒评论顺序,此示例将通过颠倒顶级评论的顺序首先显示最新的评论

wp_list_comments( array( 
'reverse_top_level' => true 
) );

  以 HTML5 格式显示评论,此示例说明如何使用 HTML5 标记格式显示评论。如果主题不支持 HTML5,它将默认为 XHTML

wp_list_comments( array( 
'format' => 'html5' 
) );

  推荐:[最新版]Asset CleanUp Pro插件免费下载WordPress性能优化插件

  推荐:WordPress函数使用手册


晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数wp_list_comments()显示帖子评论列表

转载请保留链接:https://www.pythonthree.com/wp-list-comments-2/

滚动至顶部