返回介绍

wp_list_comments()

发布于 2017-09-11 12:21:54 字数 8651 浏览 803 评论 0 收藏 0

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

List comments.


description

Used in the comments.php template to list comments for a particular post.


参数

$args

(string|array) (Optional) Formatting options.

  • 'walker'
    (object) Instance of a Walker class to list comments. Default null.
  • 'max_depth'
    (int) The maximum comments depth.
  • 'style'
    (string) The style of list ordering. Default 'ul'. Accepts 'ul', 'ol'.
  • 'callback'
    (string) Callback function to use. Default null.
  • 'end-callback'
    (string) Callback function to use at the end. Default null.
  • 'type'
    (string) Type of comments to list. Default 'all'. Accepts 'all', 'comment', 'pingback', 'trackback', 'pings'.
  • 'page'
    (int) Page ID to list comments for.
  • 'per_page'
    (int) Number of comments to list per page.
  • 'avatar_size'
    (int) Height and width dimensions of the avatar size. Default 32.
  • 'reverse_top_level'
    (bool) Ordering of the listed comments. If true, will display newest comments first.
  • 'reverse_children'
    (bool) Whether to reverse child comments in the list. Default null.
  • 'format'
    (string) How to format the comments list. Default 'html5' if the theme supports it. Accepts 'html5', 'xhtml'.
  • 'short_ping'
    (bool) Whether to output short pings. Default false.
  • 'echo'
    (bool) Whether to echo the output or return it. Default true.

Default value: array()

$comments

(array) (Optional) Array of WP_Comment objects.

Default value: null


源代码

File: wp-includes/comment-template.php

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 = $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,
	);

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

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

	// Figure out what comments we'll be looping through ($_comments)
	if ( null !== $comments ) {
		$comments = (array) $comments;
		if ( empty($comments) )
			return;
		if ( 'all' != $r['type'] ) {
			$comments_by_type = separate_comments($comments);
			if ( empty($comments_by_type[$r['type']]) )
				return;
			$_comments = $comments_by_type[$r['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 ( $r['page'] || $r['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 ( $r['page'] != $current_cpage || $r['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'] = get_current_user_id();
				} else {
					$commenter = wp_get_current_commenter();
					if ( $commenter['comment_author_email'] ) {
$comment_args['include_unapproved'] = $commenter['comment_author_email'];
					}
				}

				$comments = get_comments( $comment_args );

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

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

		// Otherwise, fall back on the comments from `$wp_query->comments`.
		} else {
			if ( empty($wp_query->comments) )
				return;
			if ( 'all' != $r['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[$r['type']]) )
					return;
				$_comments = $wp_query->comments_by_type[$r['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 ) {
					$r['cpage'] = $cpage;

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

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

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

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

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

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

	if ( null === $r['reverse_top_level'] )
		$r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );

	wp_queue_comments_for_comment_meta_lazyload( $_comments );

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

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

	$in_comment_loop = false;

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

更新日志

Versiondescription
2.7.0Introduced.

相关函数

Uses

  • wp-includes/comment.php: wp_queue_comments_for_comment_meta_lazyload()
  • wp-includes/comment-template.php: wp_list_comments_args
  • wp-includes/theme.php: current_theme_supports()
  • wp-includes/pluggable.php: is_user_logged_in()
  • wp-includes/query.php: get_query_var()
  • wp-includes/query.php: set_query_var()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/class-wp-walker.php: Walker::paged_walk()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • wp-includes/user.php: get_current_user_id()
  • wp-includes/post-template.php: get_the_ID()
  • wp-includes/comment.php: wp_get_current_commenter()
  • wp-includes/comment.php: separate_comments()
  • wp-includes/comment.php: get_comment_pages_count()
  • wp-includes/comment.php: get_comments()
  • Show 11 more uses Hide more uses

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文