函数文档

get_the_author()

💡 云策文档标注

概述

get_the_author() 函数用于检索当前文章的作者显示名称,基于全局变量 $authordata 实现。该函数已弃用参数,并返回经过 'the_author' 过滤器处理后的字符串。

关键要点

  • 函数返回当前文章作者的显示名称,若未知则返回空字符串。
  • 参数 $deprecated 已弃用,从 WordPress 2.1.0 版本起不建议使用。
  • 通过 apply_filters('the_author', $display_name) 钩子允许过滤作者显示名称。
  • 函数依赖于全局变量 $authordata,需确保在循环内或已设置该变量时调用。
  • 相关函数包括 get_the_author_posts_link()、the_author() 和 get_the_author_meta() 等。

代码示例

// 在文章循环中获取作者名称
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        $author_name = get_the_author();
        echo '作者: ' . $author_name;
    }
}

注意事项

  • 从 WordPress 6.3.0 版本起,若作者显示名称未知,函数返回空字符串而非其他值。
  • 如需获取作者完整元数据,建议使用 get_the_author_meta() 函数。
  • 确保在适当的上下文中调用,以避免 $authordata 未定义导致的错误。

📄 原文内容

Retrieves the author of the current post.

Parameters

$deprecatedstringrequired
Deprecated.

Return

string The author’s display name, empty string if unknown.

Source

function get_the_author( $deprecated = '' ) {
	global $authordata;

	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '2.1.0' );
	}

	/**
	 * Filters the display name of the current post's author.
	 *
	 * @since 2.9.0
	 *
	 * @param string $display_name The author's display name.
	 */
	return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : '' );
}

Hooks

apply_filters( ‘the_author’, string $display_name )

Filters the display name of the current post’s author.

Changelog

Version Description
6.3.0 Returns an empty string if the author’s display name is unknown.
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Grab the Author’s ‘Public’ Name
    Grabs the value in the user’s Display name publicly as field.