WP-Mix

A fresh mix of code snippets and tutorials

WordPress Get the Author Outside Loop

This tutorial explains how to get the current Post Author outside of the WordPress Loop.

For the author-archive views generated by my shapeSpace starter theme, I needed a way to display author information outside of the Loop. This article explains two ways to make it happen..

Method 1

This method uses the WP API and is the cleanest way of getting author information outside of the Loop:

<h3>Posts by: 
<?php 
	the_post(); // queue first post
	echo get_the_author(); // echo author
	rewind_posts(); // rewind the loop
?>
</h3>

..which may be simplified like this:

<h3>Posts by: <?php the_post(); echo get_the_author(); rewind_posts(); ?></h3>

As written, that code displays the author’s Display Name. To instead get the entire WP_User object, the code can be modified as follows:

<?php 
	the_post(); // queue first post
	$author_id = get_the_author_meta('ID');
	$curauth = get_user_by('id', $author_id);
	rewind_posts(); // rewind the loop
?>

Then you can call any method or property of WP_User to get whatever author information is required. Here is an advanced example of this, as featured in the shapeSpace theme:

<div id="archive-view-author">
	
	<?php 
		the_post(); // queue first post
		
		$author_id = get_the_author_meta('ID');
		$curauth = get_user_by('ID', $author_id);
		
		$user_nicename    = $curauth->user_nicename;
		$display_name     = $curauth->display_name;
		$user_description = $curauth->user_description;
		$user_email       = $curauth->user_email;
		$user_url         = $curauth->user_url;
		$user_website     = $curauth->website_name;
		$user_twitter     = $curauth->twitter;
		
		rewind_posts(); // rewind the loop
	?>
	
	<h3>Author Profile: <a href="<?php echo get_author_posts_url($author_id, $user_nicename); ?>"><?php echo $display_name; ?></a></h3>
	
	<?php if ($user_description) echo '<p>'. $user_description .'</p>'; ?>
	<?php if ($user_email)       echo get_avatar($user_email, '100'); ?>
	
	<?php if ($user_url || $user_twitter) : ?>
	
	<ul>
		
		<?php if ($user_url) : ?>
		<li>Website: <a href="<?php echo $user_url; ?>"><?php if ($user_website) : echo $user_website; else : echo $user_url; endif; ?></a></li>
		<?php endif; ?>
		
		<?php if ($user_twitter) : ?>
		<li>Twitter: <a href="https://twitter.com/<?php echo $user_twitter; ?>">@<?php echo $user_twitter; ?></a></li>
		<?php endif; ?>
		
	</ul>
	
	<?php endif; ?>
	
</div>

So pretty much you can get whatever author/user infos you need, even outside of the WordPress Loop.

Method 2

Here is an alternate method that uses the global $post object along with get_the_author_meta() to retrieve the author’s Display Name:

<?php if (is_author()) : 
	global $post;
	$author_id = $post->post_author;
	$author = get_the_author_meta('display_name', $author_id); ?>

<p class="archive-type"><span><?php _e('Posts by:', 'shapespace'); ?> <?php echo $author; ?></span></p>

To get other author details, you can change the first parameter of get_the_author_meta().

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress