get_author_posts_url()WP 2.1.0

Gets the URL (link) to the author's archive page (the page with the user's posts).

Hooks from the function

Returns

String. URL to the author's page.

Usage

get_author_posts_url( $author_id, $author_nicename );
$author_id(integer) (required)
ID of the author whose URL to the page needs to be obtained.
$author_nicename(string)
Author's universal name, nickname (slug), URL to the page that needs to be obtained. Specifying this parameter is beneficial for simplifying the function's operation, as the function will not need to retrieve the author's Nick by the provided ID.
Default: ''

Examples

0

#1 Get a link to the page with the posts of author 5, whose nickname is "batmen":

$author_url = get_author_posts_url( 5, 'batmen' );
echo $author_url;

// Outputs: http://example.com/author/batmen
0

#2 Display the link of the author page for the author of the current post

<a 
	href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" 
	title="<?php echo esc_attr( get_the_author() ); ?>"
>
	<?php the_author(); ?>
</a>

Most get_ functions are not escaped and require escaping for safe usage.

Notes

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Changelog

Since 2.1.0 Introduced.

get_author_posts_url() code WP 7.0

function get_author_posts_url( $author_id, $author_nicename = '' ) {
	global $wp_rewrite;

	$author_id = (int) $author_id;
	$link      = $wp_rewrite->get_author_permastruct();

	if ( empty( $link ) ) {
		$file = home_url( '/' );
		$link = $file . '?author=' . $author_id;
	} else {
		if ( '' === $author_nicename ) {
			$user = get_userdata( $author_id );
			if ( ! empty( $user->user_nicename ) ) {
				$author_nicename = $user->user_nicename;
			}
		}
		$link = str_replace( '%author%', $author_nicename, $link );
		$link = home_url( user_trailingslashit( $link ) );
	}

	/**
	 * Filters the URL to the author's page.
	 *
	 * @since 2.1.0
	 *
	 * @param string $link            The URL to the author's page.
	 * @param int    $author_id       The author's ID.
	 * @param string $author_nicename The author's nice name.
	 */
	$link = apply_filters( 'author_link', $link, $author_id, $author_nicename );

	return $link;
}