You have a site with several contributors. Wouldn’t it be nice to give each contributor a personalised author page which gives not only an author archive page, but also pulls in their details from their User Profile? And why not add their gravatars too, without resorting to a plugin?
That’s exactly what Candace Robinson wanted for her multi-author Arizona Real Estate Blogging Network site. Here’s a screenshot from her site which you can click to see the customised author pages, which were built using this code, in action.
So, let’s build something similar. It’s a shame that not many free and “premium” themes provide a suitable author template as standard, but by following this article you will be able to add such a template to your site, and in doing so tell your readers more about your contributors and give the latter a little boost to their egos!
As is often the case, WordPress has many built-in template tags and functions that are not often used but are very useful for adding functionality to your site. Sure enough, a quick trawl through the Codex reveals that WordPress does indeed provide a way of creating an author template page that does all we want, and is easy to implement too.
As with other articles in this series of Code Snippets I’ll give you the blocks of code necessary to create our masterpiece in the text, ready for adding to your theme files, styling and uploading to your site.
There are three steps to author page nirvana:
- Create an author.php template file
- Add some code to identify which author’s name has been clicked
- Pull in the author’s bio and other info from the User Profile
Creating an author.php template file
Most WordPress theme template files will already use the_author_posts_link() tag for generating a link to the post’s author’s recent posts. If your visitor clicks this link, WordPress’ template heirarchy will look for a suitable template file in the following order:
The chances are that your theme doesn’t have a author.php file, so normally the result of clicking the post author’s name will be a list of recent posts from the same author, which will be generated using your archive.php. As your archive.php is probably used for your category archives, we don’t want to modify this file, so our first step is to take a copy of archive.php and rename it author.php.
Displaying the author’s details
Here’s the code for pulling in the author’s bio, details and gravatar. Place this block of code in your new author.php file, just before the Loop starts. This code must be used before the Loop so that we can use some PHP functions to get the author name from the WordPress query which was originally generated by your visitor when the author’s name was clicked.
If you’re only hear to grab some code – take it! That’s all you need to get your author.php file up and running. However, if you’re not that familiar with coding, or it scares you, or even of you are a seasoned old coder and want to understand what it’s doing – read on!
So, lets’ take a minute or two to understand what this code is doing.
Line 1: The entire block of code is wrapped in a DIV so that we can apply suitable styling to this part of our new page in our stylesheet. This is always a good idea as it gives us more control over the look of our final page.
Lines 2-8: The if.. else.. endif statement finds out which author’s name has been clicked, extracts all of this author’s user profile information from the Users section of our WordPress database, and puts it into a variable called $curauth.
Now, $curauth is a very clever chap, because he contains all of the information which exists in the author’s User profile and we can now extract that information by simply using this syntax:
To use this snippet, replace $curauth->INFO with one of the following:
Take a look at your own User profile in your WordPress Admin and you should be able to work out which information is referenced by each of the above.
Now you know all this, lines 10-14 of our code should be making more sense.
Line 10 uses the WordPress function get_avatar() to get the author’s gravatar associated with his registered email address. We use $curauth->ID and size=”96″ within this function to specify whose gravatar we want (our clicked author, of course) and the size of gravatar that will be displayed, in pixels. You can read more about using gravatars in themes in another Code Snippets article Using Gravatars in WordPress themes.
The next section of code (lines 12-14) grabs the author’s nickname, his website address and his “bio” from his User profile, and displays this in our page. Of course, you can easily substitute these with any of the other $curauth properties listed above.
That’s it! We’ve made a new archive file called author.php and populated it with the author’s information from his User profile and displayed it on a personalised author archive page. Cool or what?
Clearly, I have only shown you one possible example of what can be produced, but this “barebones” example can be easily modified to suit your specific needs and will, I hope, inspire you to create your own author pages.