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.
Parameters
$use_title_attrbooloptional- Whether to add a title attribute.
Default:
true
Source
function get_the_author_link( $use_title_attr = true ) {
if ( get_the_author_meta( 'url' ) ) {
global $authordata;
$author_url = get_the_author_meta( 'url' );
$author_display_name = get_the_author();
/* translators: %s: Author's display name. */
$author_title = sprintf( __( 'Visit %s’s website' ), $author_display_name );
$link = sprintf(
'<a href="%1$s"%2$s rel="author external">%3$s</a>',
esc_url( $author_url ),
$use_title_attr ? ' title="' . esc_attr( $author_title ) . '"' : '',
$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.
This example displays the author’s Website URL as a link and the text for the link is the author’s Profile Display Name. In this example, the author’s Display Name is James Smith.
Which displays as:
Written by: James Smith