WordPress:Author Templates

来自站长百科
Xxf3325讨论 | 贡献2008年7月24日 (四) 17:40的版本 (新页面: __TOC__ Since the advent of Themes in WordPress 1.5, changing the look and feel of your WordPress site has become fairly straightforward. For instance, when a ...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

Since the advent of Themes in WordPress 1.5, changing the look and feel of your WordPress site has become 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 WordPress:Using Themes and WordPress: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 WordPress: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 WordPress:The Loop, by using the the_author_posts_link Template Tag. For example:

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

Note: As of WP 2.1, the_author_link() no longer links to author pages - it links to the URL stored in that author's Profile page instead. the_author_posts_link() is currently the only way to link to a standard WP-generated author page.

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.

NOTE: in 2.1.2 using the exclude_admin function is irrelevant when setting hide_empty=0. Setting hide_empty to zero forces the admin account to be listed as well.

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

There are also other options -- check out the WordPress:Template Tags/wp_list_authors page.

What 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 WordPress: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.php
  2. archive.php
  3. 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 isto put the following lines before WordPress:The Loop in your template file:

<?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.

<?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();
?>

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 WordPress: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. For WordPress 2.0 and higher, you can use the following:

  • $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;

And for 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;

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
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;
?>

<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