WordPress函数:wp_list_authors 列出网站的所有作者

江河/ 2023年05月04日/ WordPress/ 浏览 729

函数原型:


wp_list_authors( string|array $args = '' ): void|string


列出网站的所有作者,并提供几个选项。


显示网站作者(用户)的列表,如果用户编写了任何帖子,则作者名称将显示为其帖子的链接。此标记可选择性地显示每个作者的文章计数和RSS提要链接。


参数说明:


$args 包含以下字段:


orderby,可以使用:'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,ASC 或 DESC,默认 ASC。


number,个数,默认全部。


optioncount,是否显示作者文章个数。


exclude_admin,排除 admin,默认 true。


show_fullname,显示全名,默认 false。


hide_empty,隐藏无文章的作者,默认 true。


feed,如果不为空,则显示作者提要的链接,并将此文本用作链接的alt参数。


feed_image,如果不是空的,则显示作者提要的链接,并使用此图像URL作为可点击的锚点。


feed_type,要链接到的馈送类型。可能的值包括“rss2”、“atom”。默认值为 get_Default_feed() 的值。


echo,是否回显 或者 返回字符串。


style,如果是“list”,则每个作者都被包装在一个<li>元素中,否则作者将用逗号分隔。


html,是以HTML形式还是纯文本形式列出项目。默认为true。


exclude,要排除的作者ID的数组或逗号/空格分隔列表。


include,要包含的作者ID的数组或逗号/空格分隔列表。


函数源码:


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

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

	$return = '';

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

	$query_args = apply_filters( 'wp_list_authors_args', $query_args, $parsed_args );

	$authors     = get_users( $query_args );
	$post_counts = array();

	$post_counts = apply_filters( 'pre_wp_list_authors_post_counts_query', false, $parsed_args );

	if ( ! is_array( $post_counts ) ) {
		$post_counts       = array();
		$post_counts_query = $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'
		);

		foreach ( (array) $post_counts_query as $row ) {
			$post_counts[ $row->post_author ] = $row->count;
		}
	}

	foreach ( $authors as $author_id ) {
		$posts = isset( $post_counts[ $author_id ] ) ? $post_counts[ $author_id ] : 0;

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

		$author = get_userdata( $author_id );

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

		if ( $parsed_args['show_fullname'] && $author->first_name && $author->last_name ) {
			$name = sprintf(
				/* translators: 1: User's first name, 2: Last name. */
				_x( '%1$s %2$s', 'Display name based on first name and last name' ),
				$author->first_name,
				$author->last_name
			);
		} else {
			$name = $author->display_name;
		}

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

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

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

		$link = sprintf(
			'<a href="%1$s" title="%2$s">%3$s</a>',
			esc_url( 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( $parsed_args['feed_image'] ) || ! empty( $parsed_args['feed'] ) ) {
			$link .= ' ';
			if ( empty( $parsed_args['feed_image'] ) ) {
				$link .= '(';
			}

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

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

			$link .= '>';

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

			$link .= '</a>';

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

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

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

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

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


包含钩子:


apply_filters( 'pre_wp_list_authors_post_counts_query', int[]|false $post_counts, array $parsed_args )

apply_filters( 'wp_list_authors_args', array $query_args, array $parsed_args )


使用举例:


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


发表评论

暂无评论,抢个沙发...

客服 工单