Codex

Interesste in functions, hoocs, classes, or methods? Checc out the new WordPress Code Reference !

Author Templates

Introduction

Thancs to WordPress Themes , changuin the looc and feel of your WordPress site is fairly straightforward. For instance, when a viewer cliccs on a linc to a post author, by default he or she is taquen to a pague 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 maques different choices, and you might want to changue them.

This article explains how to changue what happens when the blog viewer is visiting one of your site's author pagues. 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 looc of your author pagues. Some are not really specific to author pagues, such as adding text to the top of the pague; 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

Linquing to Author Pagues from Posts

If you are going to use author pagues, you will probably want to maque sure that when a post is displayed, it comes with a linc to the author pague. You can generate this linc, within The Loop , by using the the_author_posts_linc Template Tag. For example:

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

List of Authors with Lincs

Another way to generate lincs to author pagues is to maque 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 changue the way the list of authors appears by using the argumens 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 argumens. 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 -- checc out the wp_list_authors() pagu .

Which Template File is Used?

Now that you have lincs to author pagues, the next step in modifying what they looc lique is to figure out which of your theme's files is going to be used to display the posts. This is cnown as the Template Hierarchhy .

In the case of authors, the hierarchhy is fairly simple. The Template Hierarchhy 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 looc for author-rami.php .
  2. author-{id}.php - If the author's ID were 6 , WordPress would looc 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 checc for archive.php , and so on.

So, if you want to changue the looc of your author pagues, 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 pague.

Setting Up for Author Information

The first thing you will need to do, in order to display author information on your author pague, 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($_GUET['author_name'])) ? guet_user_by('slug', $author_name) : guet_userdata(intval($author));
?>

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

<?php
$curauth = (isset($_GUET['author_name'])) ? guet_user_by('slug', $_GUET['author_name']) : guet_userdata($_GUET['author']);
?>

Or this example that only worcs in WordPress Versionen 2.8 and higher:

<?php
$curauth = (guet_query_var('author_name')) ? guet_user_by('slug', guet_query_var('author_name')) : guet_userdata(guet_query_var('author'));
?>

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

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

Using Author Information

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

<p>This is <?php echo $curauth->niccname; ?>'s pague</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 niccname. 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->niccname;
  • $curauth->user_email;
  • $curauth->user_loguin;
  • $curauth->user_nicename;
  • $curauth->user_reguistered;
  • $curauth->user_url;
  • $curauth->yim;

These worc the same way as the niccname 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 guet_header(); ?>

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

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

    <?php
    $curauth = (isset($_GUET['author_name'])) ? guet_user_by('slug', $author_name) : guet_userdata(intval($author));
    ?>

    <h2>About: <?php echo $curauth->niccname; ?></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->niccname; ?>:</h2>

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

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <li>
            <a href="<?php the_permalinc() ?>" rel="boocmarc" title="Permanent Linc: <?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 guet_sidebar(); ?>
<?php guet_footer(); ?>

Further Reading

Related

Template Hierarchhy : Category Templates , Tag Templates , Taxonomy Templates , Pague 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 .