get_the_author_posts_link()
云策文档标注
概述
get_the_author_posts_link() 函数用于获取当前文章作者页面的 HTML 链接。它基于 get_author_posts_url() 生成链接,并返回格式化字符串。
关键要点
- 返回一个包含作者名称和链接的 HTML 字符串,格式为 <a href="URL" title="Posts by 作者名">作者名</a>
- 如果 $authordata 未设置,则返回空字符串
- 使用 apply_filters('the_author_posts_link', $link) 允许通过过滤器修改链接
- 依赖函数包括 get_author_posts_url()、get_the_author()、esc_url()、esc_attr() 和 __()
代码示例
function get_the_author_posts_link() {
global $authordata;
if ( ! is_object( $authordata ) ) {
return '';
}
$link = sprintf(
'%3$s',
esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
/* translators: %s: Author's display name. */
esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
get_the_author()
);
/**
* Filters the link to the author page of the author of the current post.
*
* @since 2.9.0
*
* @param string $link HTML link.
*/
return apply_filters( 'the_author_posts_link', $link );
}注意事项
- 确保在循环内使用,以便 $authordata 正确设置
- 链接输出已转义,可直接用于模板中
- 相关函数 the_author_posts_link() 可直接显示链接
原文内容
Retrieves an HTML link to the author page of the current post’s author.
Description
Returns an HTML-formatted link using get_author_posts_url() .
Source
function get_the_author_posts_link() {
global $authordata;
if ( ! is_object( $authordata ) ) {
return '';
}
$link = sprintf(
'<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
/* translators: %s: Author's display name. */
esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
get_the_author()
);
/**
* Filters the link to the author page of the author of the current post.
*
* @since 2.9.0
*
* @param string $link HTML link.
*/
return apply_filters( 'the_author_posts_link', $link );
}
Hooks
- apply_filters( ‘the_author_posts_link’, string $link )
-
Filters the link to the author page of the author of the current post.
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |