Author Templates
From 워드프레스(WORDPRESS) 한국어 위키
Contents |
WordPress allows creation of a custom Author Template for your WordPress Theme. You can put information in this template file to display information about authors on the blog. All authors will use the same author.php template.
Using Author Template
The template, author.php is defined within the Template Hierarchy. It is used when an author is queried such as when a user clicks an author's link in a post. Based on the Template Hierarchy rules, WordPress automatically routes the query to the author.php template file, if one is present. Otherwise, the archive.php will be used, and if that isn’t available, the index.php will be used to fetch the list of other posts by that author. Using a custom author's template file allows customizing the information presented.
Linking to the Author Template Page
The author.php won't know what data to display if you don't pass the query to it. You need to link to the author.php, telling it which author to display. Here are some examples of such links that query the template and specify an author:
?author=2 ?author_name=joe /author/joe/
These are requesting the author page, and telling it, "give me data for Joe."
To generate these links you can use Template Tags which list authors. See: wp_list_authors() and list_authors().
These links can be placed, for example, in a custom template, archive list, or sidebar. Here is an example using wp_list_authors():
<h2>List of authors:</h2> <ul> <?php wp_list_authors(); ?> </ul>
This will generate a list of authors, and a link to each author's page in your sidebar which would look something like:
Custom Author Template
Setting the Value of Current Author
The code below sets the value for a variable called $curauth (Current Author) which will be used in the template to indicate what data to display. This first bit of code goes INTO the author.php template file. It pulls in the author ID or name, and assigns that value to $curauth:
<?php if(isset($_GET['author_name'])) : $curauth = get_userdatabylogin($author_name); // NOTE: 2.0 bug requires get_userdatabylogin(get_the_author_login()); else : $curauth = get_userdata(intval($author)); endif; ?>
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 both WordPress Version 1.2 and WordPress Version 1.5 and higher. NOTE: this does not work in 2.0 due to this bug Bug ist marked as closed.
<?php if(isset($_GET['author_name'])) : $curauth = get_userdatabylogin($_GET['author_name']); else : $curauth = get_userdata($_GET['author']); endif; ?>
Or this example that only works in WordPress Version 1.5 and higher:
<?php
if(get_query_var('author_name')) :
$curauth = get_userdatabylogin(get_query_var('author_name'));
else :
$curauth = get_userdata(get_query_var('author'));
endif;
?>
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(); ?>
Note: All these Versions show you the current (meaning just quearried) author. If you want to display your own profile (logged in author) use this one line of code. Testet in WP 2.0.2: I placed it in my header.php so I can use it all over my templates.
<?php // Get the current user-variable. // use "$current_user->first_name" for example // look at the array with print_r() for all avaliable names global $current_user; // for testing use //print_r($current_user); ?>
Using Current Author Outside of the Loop
Once you have the $curauth value, you can use it to get other data from the author profile. You must use it outside of the Loop. For example:
<p>This is <?php echo $curauth->nickname; ?>'s page</p>
Will return:
This is Joe's page
Listing Entries via The Loop
By using the loop in author.php, you can list all entries for the queried author. This will list all posts from the queried author:
- The title in the h3
- The date
- The content and a read more link
- A comments link
- And if there are no posts, it will say so
The Loop code looks like this:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h3>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<small><?php the_time('F jS, Y') ?> </small>
<?php the_content('Read the rest of this entry »'); ?>
<p>
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
</p>
<?php endwhile; ?>
<?php else : ?>
<p>No posts by this author</p>
<?php endif; ?>
About Current Author
It is important to know that $curauth is not a WordPress variable, but a variable generated by PHP code and sent via a link as a query. Therefore, the author.php file must be requested via a link to the author page, which specifies an author. If you request the author.php page directly, you'll end up with no data.
Here is all the user data you can access through $curauth:
- $curauth->aim;
- $curauth->description;
- $curauth->display_name;
- $curauth->first_name;
- $curauth->icq; (not editable since WP 2.0)
- $curauth->ID;
- $curauth->jabber;
- $curauth->last_name;
- $curauth->msn; * (not editable since WP 2.0)
- $curauth->nickname;
- $curauth->user_email;
- $curauth->user_login;
- $curauth->user_nicename;
- $curauth->user_registered;
- $curauth->user_url;
- $curauth->wp_capabilities; Note: wp_ represents your database prefix
- $curauth->wp_user_level; Note: wp_ represents your database prefix
- $curauth->yim;
Here are examples of how it would be used on the author.php page:
- <?php echo $curauth->nickname; ?> - Displays the user's nickname.
- <?php echo $curauth->description; ?> - Displays the user's profile information (as filled in from their user account.)
These are values returned by get_userdata and get_userdatabylogin in WordPress 1.2 and 1.5
- $curauth->user_aim;
- $curauth->user_description;
- $curauth->user_email;
- $curauth->user_firstname;
- $curauth->user_icq;
- $curauth->user_lastname;
- $curauth->user_level;
- $curauth->user_login;
- $curauth->user_msn;
- $curauth->user_nickname;
- $curauth->user_url;
- $curauth->user_yim;
Custom Author Templates Examples
The following is a list of instructional examples for linking to and creating custom author templates.
Linking to Author Page from Sidebar
To list authors in your sidebar, you need to create a list of author links. When the visitor clicks on an author's name, it brings up a page listing all posts by that author. The list will have a link to each post, show the date, and show the post's Category. This example will work with the default WordPress theme.
First, generate a list of authors and links in the sidebar.php template file, then prepare the author.php to display the list of posts.
- Open sidebar.php in a text editor.
We can generate a list of authors, and links by using the default settings available from wp_list_authors(). Note: You could add this to other template files such as archives.php.<h2>List of authors:</h2> <ul> <?php wp_list_authors(); ?> </ul>
For more information, see wp_list_authors() and list_authors(). - Create a new blank text file, and save it as author.php in your Theme directory. Paste the following code into the new author.php file (this will include the Theme's header, sidebar and footer):
<?php get_header(); ?> <div id="content" class="narrowcolumn"> <!-- This sets the $curauth variable --> <?php if(isset($_GET['author_name'])) : $curauth = get_userdatabylogin($author_name); else : $curauth = get_userdata(intval($author)); endif; ?> <h2>Posts by <?php echo $curauth->user_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(); ?>
Including Profile Information
You can include more profile information, outside of The Loop, using the variables described above for $curauth. For example, before The Loop that displays the lists of posts, add the following:
<h2>About: <?php echo $curauth->user_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>
Linking Options
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. Note that users with no posts are not displayed as a link.
<ul>
<?php wp_list_authors('exclude_admin=0&hide_empty=0'); ?>
</ul>
Author Data in Other Templates
You can include author data in other templates.
For example, if you use the Template Tag the_author_posts_link() within The Loop, clicking on it will bring the visitor to a list of all posts by a specified author. Again, based on the Template Hierarchy, this will use the archive.php unless author.php is present.
Therefore, you can choose to include the author data on an Archive Index. You can have it read, for example: "Harriett Smith's page of posts". You'll need the same $curauth code at the top to pull in the current author data, and you'll need to print the page title.
But, if your Archive Index is used for different purposes, you'll want to check first if this archive is being access from an author link? To do this, use an if() statement to check if the author data was passed. Then you can assign the variable, and print $curauth data where you like. This code below will accomplish that for you.
<!-- check if author is passed (if it was an author link to archive), then define the $curauth variable --> <?php if(isset($_GET['author_name']) || isset($_GET['author'])) : if($_GET['author_name']) : $curauth = get_userdatabylogin($_GET['author_name']); else : $curauth = get_userdata($_GET['author']); endif; ?> <!-- print the page title, including author's name, only if above check is true --> <h3>You're currently viewing <?php echo $curauth->first_name; ?> <?php echo $curauth->last_name; ?>'s page of posts:</h3> <?php endif; ?>
References
- Kafkaesquí's Get Author Profile Plugin will also access the author information and allows you to provide author information outside of the Loop, such as for a sidebar intro of the blog owner or to list contributors to your blog.
- WordPress Support Forum Thread on Author Templates
- Another WordPress Support Forum Thread on Author Templates
This article is marked as in need of editing. You can help Codex by editing it.

