» WordPress Coding & Design » Creating Author Pages

SarahCreating Author Pages

Written by Sarah from Stuff By Sarah on November 16, 2008

If you’re running a multi author WordPress site then at some point you’re going to want to have a list of the authors on your site, and give them their own page. All Blogging Tips authors have their own author page for example, which gives a brief bit of information about the author plus a list the posts they’ve written - view mine.

Creating an author page is not as straightforward as other template files, however once you know the right few lines of code to get started with, the rest is simple.

Link to the Author

Before we create a page for the author, we need to create a link to the author’s page. This is done within the post meta (or anywhere within the loop for a post) using the tag

<?php the_author_posts_link(); ?>

This will print out the name of the post author and the name will also be linked through to the author’s page. So you could precede this with ‘Written By’ for example.

The Author Template

Now we need an author template page. This is powered via the theme file author.php. If author.php doesn’t exist then using the template hierarchy, WordPress will use archive.php, and if this doesn’t exist, it will use index.php. So the best way to start is to duplicate your archive.php file and save it as author.php.

Next you need to find your loop within this file and then above this we can start adding the code needed.

Get the Author

To determine which author is being viewed we need the following code to be placed above the loop:

< ?php
global $wp_query;
$curauth = $wp_query->get_queried_object();
?>

This will then assign the author into the variable $curauth. We then use this in certain tags to then display the information we want. Anything entered into the Admin User screen can be retrieved and displayed on the front end (with the exception of the password of course!). The different tags available are listed below with their relevant field names in the Edit Profile admin page.

$curauth->aim;
AIM
$curauth->description;
Biographical Info
$curauth->display_name;
Display Name Publicly As
$curauth->first_name;
First name
$curauth->ID;
The User ID (uneditable)
$curauth->jabber;
Jabber / Google Talk
$curauth->last_name;
Last name
$curauth->nickname;
Nickname
$curauth->user_email;
E-mail
$curauth->user_login;
Username
$curauth->user_nicename;
User Nicename (usually the nickname in lowercase)
$curauth->user_registered;
The date the User account was first registered
$curauth->user_url;
Website
$curauth->yim;
Yahoo IM

To display this information you need to use PHP echo or print eg.

Full Name: <?php echo $curauth->first_name." ".$curauth->last_name; ?>

Of course there are some details listed above that I wouldn’t recommend displaying on an Author’s page for security. The user ID and username are two such items.

You can place these variables into standard markup (of course surrounded by PHP tags and using the echo as above), and you should place this above the loop but after the first piece of code where we defined the $curauth.

List the Author’s Posts

On an author’s page you’ll also want to list out the posts that the author has written. This is where the loop comes back into use. You’ve got the option of using the same listing as the archives page. If this is the case then keeping the loop code (which starts at if (have_posts()) : ) intact from the archive.php page should be fine. However, rather than listing the post content or excerpt, I would recommend to just list the post titles and perhaps the day they were created on (maybe the comments too!).

I’ve written about how to index your posts on your archives and categories pages, so the same method can be applied here. We simply add

query_posts($query_string.'&posts_per_page=-1');

within the PHP tags, right before the opening loop line ie.

if ( have_posts() ) :

or

if ( have_posts() ) : while ( have_posts() ) : the_post();

(different themes may vary).

This will set all posts to display on one page thereby creating an index of posts for that user. Of course you’ll need to edit the tags within the loop to prevent the content or excerpt from being displayed. To do this I would recommend the following loop code

< ?php
query_posts($query_string.'&posts_per_page=-1');
if (have_posts()) :
    echo "<ul>\n";
    while (have_posts()) : the_post(); ?>
        <li>< ?php the_time('d/m/Y') ?>: <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title(); ?>">< ?php the_title(); ?></a></li>
    < ?php endwhile;
    echo "</ul>\n";
else: ?>
    <p>< ?php _e('Sorry, no posts matched your criteria.'); ?></p>
< ?php endif; ?>

The Full Author Page

So now we have the pieces of our page, we need to fit them together. A typical example of an Author page could be

< ?php
	global $wp_query;
	$curauth = $wp_query->get_queried_object();
?>
<h2>More Info On Sarah</h2>

<dl>
<dt>Full Name :</dt> <dd>< ?php echo $curauth->first_name." ".$curauth->last_name; ?></dd>
<dt>Website :</dt> <dd><a href="<?php echo $curauth->user_url ?>">< ?php echo $curauth->user_url ?></a></dd>
</dl>

<h3>Info :</h3>
<p>< ?php echo $curauth->description; ?></p>

<h3>Posts by < ?php echo $curauth->first_name ?>: < ?php the_author_posts(); ?></h3>

< ?php
query_posts($query_string.'&posts_per_page=-1');
if (have_posts()) :
    echo "<ul>\n";
    while (have_posts()) : the_post(); ?>
        <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title(); ?>">< ?php the_title(); ?></a>, < ?php the_time('d M Y') ?> in < ?php the_category(', ') ?>. < ?php comments_popup_link('', '(1)', '(%)'); ?></li>
    < ?php endwhile;
    echo "</ul>\n";
else: ?>
    <p>< ?php _e('Sorry, no posts matched your criteria.'); ?></p>
< ?php endif; ?>

This would then give a page similar to the below:

Author Example Page

Written by Sarah from Stuff By Sarah on November 16, 2008 | Filed Under WordPress Coding & Design

Share with others

  • StumbleUpon
  • Add to Delicious
  • Mixx
Make money with LinkXL

One Response so far | Have Your Say!

  1. Farrhad  |  November 17th, 2008 at 11:32 am #

    Farrhad - Gravatar

    Another post that will help me :)

Trackbacks to 'Creating Author Pages'

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums