WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Author Templates

Introduction

Thanks to WordPress Themes, changing the look and feel of your WordPress site is fairly straightforward. For instance, when a viewer clicks on a link to a post author, by default he or she is taken to a page listing the posts from that particular author in chronological order, from newest posts at the top to oldest at the bottom. There are many display choices, including whether to display the complete post or post excerpts, and what additional information to display (title, category, publish date, last modified time, etc.). Each theme makes different choices, and you might want to change them.

This article explains how to change what happens when the blog viewer is visiting one of your site's author pages. This involves the use of Themes and Template files, so if you are new to template files, you might want to read Using Themes and Stepping Into Templates first.

There are many ways that you can modify the look of your author pages. Some are not really specific to author pages, such as adding text to the top of the page; you can read about such simple modifications in the Category Templates article. This article will concentrate on modifications that are specific to author template files.

The Basics

Linking to Author Pages from Posts

If you are going to use author pages, you will probably want to make sure that when a post is displayed, it comes with a link to the author page. You can generate this link, within The Loop, by using the the_author_posts_link Template Tag. For example:

<p>Written by: 
<?php the_author_posts_link(); ?></p>

List of Authors with Links

Another way to generate links to author pages is to make an author list in your sidebar (or elsewhere in your Theme). The wp_list_authors Template Tag does that. Just place the following in your sidebar Template file:

<h2>List of authors:</h2>
<ul>
<?php wp_list_authors(); ?>
</ul>

You may want to change the way the list of authors appears by using the arguments in wp_list_authors(). For example, the administrator account (Username "admin") is excluded by default, but you can force wp_list_authors() to include the admin account this way:

<ul>
<?php wp_list_authors('exclude_admin=0'); ?>
</ul>

You can also combine arguments. By default, authors without posts are ignored, but in this example, all authors (users), including the administrator, are displayed.

<ul>
<?php wp_list_authors('exclude_admin=0&hide_empty=0'); ?>
</ul>

There are also other options -- check out the wp_list_authors() page.

Which Template File is Used?

Now that you have links to author pages, the next step in modifying what they look like is to figure out which of your theme's files is going to be used to display the posts. This is known as the Template Hierarchy.

In the case of authors, the hierarchy is fairly simple. The Template Hierarchy specifies that WordPress uses the first Template file it finds in your current Theme's directory from the following list:

  1. author-{nicename}.php - If the author's nice name were rami, WordPress would look for author-rami.php.
  2. author-{id}.php - If the author's ID were 6, WordPress would look for author-6.php.
  3. author.php
  4. archive.php
  5. index.php

That is, if you do not have an author.php file, WordPress will check for archive.php, and so on.

So, if you want to change the look of your author pages, you need to create an author.php file if it doesn't exist, by copying archive.php if that file exists, or index.php if it doesn't. The rest of the article assumes you are editing author.php.

Custom Author Information

This section explores how to add information about the author, such as name, bio, and contact information, to an author page.

Setting Up for Author Information

The first thing you will need to do, in order to display author information on your author page, is edit your author template file(author.php, see above) so that it figures out which author is being viewed, and retrieves all the information about the author from the database (i.e. the information entered in the User administration screen of WordPress).

This is done by setting up a variable called $curauth (Current Author). The usual way to do this is to put the following lines before The Loop in your template file:

<?php 
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>

There are other ways to receive the query and assign the value of $curauth, if the above does not work for you. For example try this code which should work in WordPress Version 2.8 and higher.

<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $_GET['author_name']) : get_userdata($_GET['author']);
?>

Or this example that only works in WordPress Version 2.8 and higher:

<?php
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
?>

If the above fails to work for you, another option for WordPress 1.5 or above is the following:

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

Using Author Information

Now that you have the $curauth variable set up, you can use it to display all kinds of information about the author whose page is being displayed. For example, to display the author's nickname, in a format like "This is Joe's page", you could use:

<p>This is <?php echo $curauth->nickname; ?>'s page</p>

Note that this must be placed after defining $curauth as in the previous section, and before The Loop in your Template file.

There are many other pieces of information you can display, besides the author's nickname. All of these come from the WordPress user editing screen:

  • $curauth->aim;
  • $curauth->description;
  • $curauth->display_name;
  • $curauth->first_name;
  • $curauth->ID;
  • $curauth->jabber;
  • $curauth->last_name;
  • $curauth->nickname;
  • $curauth->user_email;
  • $curauth->user_login;
  • $curauth->user_nicename;
  • $curauth->user_registered;
  • $curauth->user_url;
  • $curauth->yim;

These work the same way as the nickname example above. For instance, to display the author's displayed name and description (i.e. "About Yourself" text):

<p><?php echo $curauth->display_name; ?><br />
<?php echo $curauth->description; ?></p>

Sample Template File

Here is a complete sample author.php file, which you can use as an example:

<?php get_header(); ?>

<div id="content" class="narrowcolumn">

<!-- This sets the $curauth variable -->

    <?php
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
    ?>

    <h2>About: <?php echo $curauth->nickname; ?></h2>
    <dl>
        <dt>Website</dt>
        <dd><a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a></dd>
        <dt>Profile</dt>
        <dd><?php echo $curauth->user_description; ?></dd>
    </dl>

    <h2>Posts by <?php echo $curauth->nickname; ?>:</h2>

    <ul>
<!-- The Loop -->

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <li>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
            <?php the_title(); ?></a>,
            <?php the_time('d M Y'); ?> in <?php the_category('&');?>
        </li>

    <?php endwhile; else: ?>
        <p><?php _e('No posts by this author.'); ?></p>

    <?php endif; ?>

<!-- End Loop -->

    </ul>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Further Reading

Related

Template Hierarchy: Category Templates, Tag Templates, Taxonomy Templates, Page Templates, Post Type Templates, Author Templates, Date Templates, Search Templates, 404 Templates, Attachment Templates, Loop Templates

See also index of Function Reference and index of Template Tags.