函数文档

get_the_author_link()

💡 云策文档标注

概述

get_the_author_link() 函数用于获取作者链接或作者名称。如果作者设置了主页 URL,则返回 HTML 链接,否则返回作者名称。

关键要点

  • 函数返回字符串:如果作者 URL 存在于用户元数据中,则返回包含作者名称的 HTML 链接,否则返回 get_the_author() 的结果。
  • 函数内部使用 get_the_author_meta('url') 检查作者 URL,并应用过滤器 'the_author_link' 以允许自定义链接 HTML。
  • 相关函数包括 get_the_author_meta()、get_the_author()、__()、esc_url()、esc_attr() 和 apply_filters()。

代码示例

function get_the_author_link() {
    if ( get_the_author_meta( 'url' ) ) {
        global $authordata;

        $author_url          = get_the_author_meta( 'url' );
        $author_display_name = get_the_author();

        $link = sprintf(
            '%3$s',
            esc_url( $author_url ),
            /* translators: %s: Author's display name. */
            esc_attr( sprintf( __( 'Visit %s’s website' ), $author_display_name ) ),
            $author_display_name
        );

        /**
         * Filters the author URL link HTML.
         *
         * @since 6.0.0
         *
         * @param string  $link       The default rendered author HTML link.
         * @param string  $author_url Author's URL.
         * @param WP_User $authordata Author user data.
         */
        return apply_filters( 'the_author_link', $link, $author_url, $authordata );
    } else {
        return get_the_author();
    }
}

注意事项

  • 函数自 WordPress 3.0.0 版本引入,可通过过滤器 'the_author_link' 自定义输出。
  • 使用示例:在主题中调用 the_author_link() 可直接显示作者链接或名称,例如显示为 "Written by: James Smith"。

📄 原文内容

Retrieves either author’s link or author’s name.

Description

If the author has a home page set, return an HTML link, otherwise just return the author’s name.

Return

string An HTML link if the author’s URL exists in user meta, otherwise the result of get_the_author() .

Source

function get_the_author_link() {
	if ( get_the_author_meta( 'url' ) ) {
		global $authordata;

		$author_url          = get_the_author_meta( 'url' );
		$author_display_name = get_the_author();

		$link = sprintf(
			'<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
			esc_url( $author_url ),
			/* translators: %s: Author's display name. */
			esc_attr( sprintf( __( 'Visit %s’s website' ), $author_display_name ) ),
			$author_display_name
		);

		/**
		 * Filters the author URL link HTML.
		 *
		 * @since 6.0.0
		 *
		 * @param string  $link       The default rendered author HTML link.
		 * @param string  $author_url Author's URL.
		 * @param WP_User $authordata Author user data.
		 */
		return apply_filters( 'the_author_link', $link, $author_url, $authordata );
	} else {
		return get_the_author();
	}
}

Hooks

apply_filters( ‘the_author_link’, string $link, string $author_url, WP_User $authordata )

Filters the author URL link HTML.

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes