get_the_modified_author()
云策文档标注
概述
get_the_modified_author() 函数用于检索最后编辑当前文章的作者显示名称。它接受一个可选的 $post 参数,并返回字符串或 null 值。
关键要点
- 参数 $post 可选,可以是文章 ID、WP_Post 对象或 null,默认为全局 $post 对象。
- 返回值为字符串(作者显示名称)或 null(若无最后编辑者或文章无效),空字符串表示用户不可用。
- 内部通过 get_post_meta() 获取 _edit_last 元数据,再使用 get_userdata() 检索用户信息。
- 提供 the_modified_author 过滤器钩子,允许自定义显示名称。
- 从版本 6.9.0 起添加 $post 参数,并明确返回 null 而非 void。
代码示例
// 基本用法:获取当前文章的最后编辑作者
$author = get_the_modified_author();
// 指定文章 ID 获取最后编辑作者
$author = get_the_modified_author( 123 );
// 使用过滤器自定义显示名称
add_filter( 'the_modified_author', function( $display_name ) {
return '编辑者: ' . $display_name;
} );注意事项
- 确保文章存在,否则函数可能返回 null。
- 返回值可能为空字符串,表示用户数据不可用,需在代码中处理此情况。
- 相关函数包括 the_modified_author()(用于直接显示)和 get_userdata()、get_post_meta() 等。
原文内容
Retrieves the author who last edited the current post.
Parameters
$postint|WP_Post|nulloptional-
Post ID or post object. Default is global
$postobject.Default:
null
Source
function get_the_modified_author( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return null;
}
$last_id = get_post_meta( $post->ID, '_edit_last', true );
if ( ! $last_id ) {
return null;
}
$last_user = get_userdata( $last_id );
/**
* Filters the display name of the author who last edited the current post.
*
* @since 2.8.0
*
* @param string $display_name The author's display name, empty string if user is unavailable.
*/
return apply_filters( 'the_modified_author', $last_user ? $last_user->display_name : '' );
}
Hooks
- apply_filters( ‘the_modified_author’, string $display_name )
-
Filters the display name of the author who last edited the current post.