函数文档

the_author()

💡 云策文档标注

概述

the_author() 是一个 WordPress 模板标签,用于显示当前文章的作者名称。它基于旧功能设计,主要用于输出 get_the_author() 的返回值,以保持向后兼容性。

关键要点

  • the_author() 默认行为是直接输出作者名称,而非返回字符串。
  • 该函数有两个已弃用的参数:$deprecated 和 $deprecated_echo,建议使用 get_the_author() 替代。
  • 函数内部调用 get_the_author() 来获取作者显示名称,并处理弃用参数的警告。

代码示例

// 在主题文件中使用 the_author() 显示作者名称
echo 'This post was written by ';
the_author();

注意事项

  • the_author() 主要用于输出,如果需要返回值,应使用 get_the_author()。
  • 参数 $deprecated_echo 设置为 false 时已弃用,会触发 _deprecated_argument() 警告。
  • 作者名称格式取决于用户在个人资料中设置的“公开显示为”选项。

📄 原文内容

Displays the name of the author of the current post.

Description

The behavior of this function is based off of old functionality predating get_the_author() . This function is not deprecated, but is designed to echo the value from get_the_author() and as an result of any old theme that might still use the old behavior will also pass the value from get_the_author() .

The normal, expected behavior of this function is to echo the author and not return it. However, backward compatibility has to be maintained.

See also

Parameters

$deprecatedstringrequired
Deprecated.
$deprecated_echobooloptional
Deprecated. Use get_the_author() . Echo the string or return it.

Default:true

Return

string The author’s display name, from get_the_author() .

Source

function the_author( $deprecated = '', $deprecated_echo = true ) {
	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '2.1.0' );
	}

	if ( true !== $deprecated_echo ) {
		_deprecated_argument(
			__FUNCTION__,
			'1.5.0',
			sprintf(
				/* translators: %s: get_the_author() */
				__( 'Use %s instead if you do not want the value echoed.' ),
				'<code>get_the_author()</code>'
			)
		);
	}

	if ( $deprecated_echo ) {
		echo get_the_author();
	}

	return get_the_author();
}

Changelog

Version Description
0.71 Introduced.

User Contributed Notes