返回介绍

wp_list_authors()

发布于 2017-09-11 12:21:35 字数 7753 浏览 1105 评论 0 收藏 0

wp_list_authors( string|array $args = '' )

List all the authors of the site, with several options available.


description


参数

$args

(string|array) (Optional) Array or string of default arguments.

  • 'orderby'
    (string) How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered', 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name', 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
  • 'order'
    (string) Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
  • 'number'
    (int) Maximum authors to return or display. Default empty (all authors).
  • 'optioncount'
    (bool) Show the count in parenthesis next to the author's name. Default false.
  • 'exclude_admin'
    (bool) Whether to exclude the 'admin' account, if it exists. Default false.
  • 'show_fullname'
    (bool) Whether to show the author's full name. Default false.
  • 'hide_empty'
    (bool) Whether to hide any authors with no posts. Default true.
  • 'feed'
    (string) If not empty, show a link to the author's feed and use this text as the alt parameter of the link.
  • 'feed_image'
    (string) If not empty, show a link to the author's feed and use this image URL as clickable anchor.
  • 'feed_type'
    (string) The feed type to link to, such as 'rss2'. Defaults to default feed type.
  • 'echo'
    (bool) Whether to output the result or instead return it. Default true.
  • 'style'
    (string) If 'list', each author is wrapped in an <li> element, otherwise the authors will be separated by commas.
  • 'html'
    (bool) Whether to list the items in HTML form or plaintext. Default true.
  • 'exclude'
    (array|string) Array or comma/space-separated list of author IDs to exclude.
  • 'include'
    (array|string) Array or comma/space-separated list of author IDs to include.

Default value: ''


返回值

(string|void) The output, if echo is set to false.


源代码

File: wp-includes/author-template.php

function wp_list_authors( $args = '' ) {
	global $wpdb;

	$defaults = array(
		'orderby' => 'name', 'order' => 'ASC', 'number' => '',
		'optioncount' => false, 'exclude_admin' => true,
		'show_fullname' => false, 'hide_empty' => true,
		'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
		'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
	);

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

	$return = '';

	$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
	$query_args['fields'] = 'ids';
	$authors = get_users( $query_args );

	$author_count = array();
	foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author" ) as $row ) {
		$author_count[$row->post_author] = $row->count;
	}
	foreach ( $authors as $author_id ) {
		$author = get_userdata( $author_id );

		if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
			continue;
		}

		$posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;

		if ( ! $posts && $args['hide_empty'] ) {
			continue;
		}

		if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
			$name = "$author->first_name $author->last_name";
		} else {
			$name = $author->display_name;
		}

		if ( ! $args['html'] ) {
			$return .= $name . ', ';

			continue; // No need to go further to process HTML.
		}

		if ( 'list' == $args['style'] ) {
			$return .= '<li>';
		}

		$link = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>',
			get_author_posts_url( $author->ID, $author->user_nicename ),
			/* translators: %s: author's display name */
			esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
			$name
		);

		if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
			$link .= ' ';
			if ( empty( $args['feed_image'] ) ) {
				$link .= '(';
			}

			$link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';

			$alt = '';
			if ( ! empty( $args['feed'] ) ) {
				$alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
				$name = $args['feed'];
			}

			$link .= '>';

			if ( ! empty( $args['feed_image'] ) ) {
				$link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
			} else {
				$link .= $name;
			}

			$link .= '</a>';

			if ( empty( $args['feed_image'] ) ) {
				$link .= ')';
			}
		}

		if ( $args['optioncount'] ) {
			$link .= ' ('. $posts . ')';
		}

		$return .= $link;
		$return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
	}

	$return = rtrim( $return, ', ' );

	if ( ! $args['echo'] ) {
		return $return;
	}
	echo $return;
}

更新日志

Versiondescription
1.2.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/formatting.php: esc_url()
  • wp-includes/pluggable.php: get_userdata()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/functions.php: wp_array_slice_assoc()
  • wp-includes/link-template.php: get_author_feed_link()
  • wp-includes/user.php: get_users()
  • wp-includes/post.php: get_private_posts_cap_sql()
  • wp-includes/author-template.php: get_author_posts_url()
  • wp-includes/wp-db.php: wpdb::get_results()
  • Show 6 more uses Hide more uses

Used By

  • wp-includes/deprecated.php: list_authors()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Authors full names and number of posts
    This example displays a list of the site’s authors with the full name (first and last name) plus the number of posts for each author. Note the output is in descending order by the post count, and limited to three (3) authors/users. Also, and by default, it excludes the admin author, hides authors with no posts, and does not display the RSS feed or image.

    
    wp_list_authors( array(
    	'show_fullname' => 1,
    	'optioncount'   => 1,
    	'orderby'       => 'post_count',
    	'order'         => 'DESC',
    	'number'        => 3
    ) );
    

    Example output:

    Andrew Anderson (48)
    Harriett Smith (42)
    Sally Smith (29)

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

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

发布评论

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