author-footer

If you have a WordPress blog with multiple authors, you may find that it would be helpful to display an author blurb/bio somewhere on each post. Here are some instructions to do this using the get_template_part() function. I am also including instructions on how to update your author contact methods, since the fields built in by WordPress are a bit outdated (AIM, YIM, Jabber).

Change the Author Contact Methods

The first thing we’ll do is update the author contact methods. These are fields that are filled in by the user in their User Profile in the back end. By default, they include the user’s e-mail (required), website, AIM, Yahoo IM, and Jabber/Google Talk. For this example, I’m going to replace AIM, YIM, and Jabber with Facebook, Twitter, Google+, and LinkedIn, but you can use whichever networks you’d prefer.

Open your theme’s functions.php file and add the following:

// Update Author Contact Info
function change_contact_info($contactmethods) {
	unset($contactmethods['aim']);
	unset($contactmethods['yim']);
	unset($contactmethods['jabber']);
	$contactmethods['facebook'] = 'Facebook URL';
	$contactmethods['googleplus'] = 'Google+ URL';
	$contactmethods['twitter'] = 'Twitter Username';
	$contactmethods['linkedin'] = 'LinkedIn Username';

	return $contactmethods;
}
add_filter('user_contactmethods', 'change_contact_info');

Note that I am prompting the user for full URIs for Facebook and Google+ versus usernames for twitter and LinkedIn. This is done because of the way these networks currently format their users’ profile URIs.

Some Facebook users have set a pretty URI, but some have not: therefore, we cannot rely on this always being the case, so to be safe, we’re asking for the full URI. This also allows flexibility for people to provide links to FB pages rather than strictly user profiles.

Google+ uses a numerical ID, but since this is a very long string of numbers, it’s probably easier for users to simply copy and paste the entire URI. In addition, they now also have the option to use the links provided by short URL services like gplus.to if they’ve chosen to set something like that up.

It’s important to make note of this because it will affect how you need to display the author meta information in your theme files.

Create an Author Bio template part

Now we want to take the author meta information and display it on a per-post basis. To do this, we’ll create a template part, aka WordPress’s tag for a PHP include.

Create a new PHP file in your theme and name it bio-author.php. This can be called something else if you prefer, but I named it thusly to make sure it isn’t going to interfere with the naming schemes of any of the other theme files.

Note: Do not name your template part author-bio.php! This will cause WordPress to interpret the file as the Author page for a user with the nicename “Bio”. If you need a refresher on how the WordPress theme hierarchy works, take a look at the WordPress Template Hierarchy Map cheat sheet.

In your bio-author.php file, write the code to display the author bio you want to include on each post.

<div id="author-bio" class="author-bio">
	<?php echo get_avatar( get_the_author_meta('ID'), 80 ); ?>
	<p class="author-name">
		Author: <strong><?php the_author_posts_link(); ?></strong>
		<?php if (get_the_author_meta('user_url')) {
			echo '| <a href="' . get_the_author_meta('user_url') . '">website</a>';
		}
		if (get_the_author_meta('twitter')) { 
			echo '| <a href="http://twitter.com/' . get_the_author_meta('twitter') . '">twitter</a>';
		} ?>
	</p>
	<p class="author-description"><?php the_author_meta(user_description); ?></p>
</div>

The above code will print the author’s Gravatar and public display name with a link to their author page. If the user has filled in a website URL and twitter username, it will display these with links, separated by pipes. It then displays the user’s description.

You can edit this code to display other parts of the author meta by using the the_author_meta() template tag (displays the information) and the get_the_author_meta() function (returns the information but does not display it, useful for creating links to the new contact methods we set up).

Include the template part on single posts

We’re now ready to include the author bio with our posts. Go to your single.php and/or single-{post-type}.php file(s) and place the template part where you’d like the bio to be included:

<?php get_template_part('bio','author'); ?>

Note: This must be placed inside the loop, otherwise it will not know who the author is and will not work. I like to place it between the post content and the comments.

As you can see, the get_template_part() function works by using the parts of the hyphenated file name to include the appropriate file. You can see how this would be useful if you group your theme’s template parts by related pieces (this can be seen in action in the default WordPress TwentyEleven theme as well as my HTML5 Blank Theme).

Finally, use your theme’s styles.css to style the author bio appropriately.

One more thing: Author Pages

If your theme does not have an Author template (author.php), you should look into creating one. The Author Templates entry in the WordPress Codex will help you get started. You can also ensure that an author’s custom post types are listed on their page by using the method here: WP Authors Custom Post Types.