Displaying Wordpress Authors

26th February 2009 - 5 minutes read time

Wordpress has a couple of rarely used functions that allow author information to be displayed for the current post and a list of all of the authors on the blog.

Adding a written by message to your posts is not difficult at all. Just use the the_author_posts_link() function inside the post loop.

<?php the_author_posts_link(); ?>

This function shouldn't be confused with the the_author_link() function that prints out the link in the author's profile.

The second way of printing out author information is by using the wp_list_authors() function. This can take a number of arguments, but the simplest use of it is as follows:

  1. <ul>
  2. <?php wp_list_authors(); ?>
  3. </ul>

This should print out a list of the authors on your blog. There is one very important thing I should mention here. This function will only print out an authors name if they have written a post and are not the default admin account. I tried to print out the authors on a test blog and after a few minutes of frustration I looked at the function call itself on line 460 of the file author-template.php in the directory wp-includes. The function specifically excludes authors that haven't written any posts but you can turn this on if you give it the right parameter.

The wp_list_authors() function takes a number of arguments in the form of a string of arguments. The available arguments are as follows:

  • optioncount: Print out the number of posts for each author. The default is 0 (false).
  • exclude_admin: Print out the blog admin user. The default is 1 (true)
  • show_fullname: Use the full name of the author instead of their nickname. Default is 0 (false).
  • hide_empty: Show users who don't have any posts. The default is 1 (true). Note that even if a user with no posts is printed they will not be linked.
  • echo: Immediately echo the list of authors or return as a variable. The default is 1 (true).
  • feed: If set then a link to an RSS feed for that author is printed, along with the text given. The default is not to print anything.
  • feed_image: This is a link to a RSS icon, it works in the same way as the feed parameter, but will override it.

To use the arguments you need to pass them to the function in the form of a string or arguments like this.

<?php wp_list_authors('exclude_admin=0&hide_empty=0&feed=Feed'); ?>

Now that you have a link of authors being printed out it is time to look at where the links go to. Each author has an ID associated with them, and this is appended to the URL using the author tag. If you have permalinks turned on then the authors name is used.

When clicked on Wordpress will attempt to display the author information using a template file called author.php. If this is not present then it will display a list of the posts using the archive.php template file. Finally, if this is not present then Wordpress will default to the index file.

Your author.php file should contain the following lines so that you can access the author information.

  1. <?php
  2. if(isset($_GET['author_name'])) :
  3. $curauth = get_userdatabylogin($author_name);
  4. else :
  5. $curauth = get_userdata(intval($author));
  6. endif;
  7. ?>

The $curauth variable now contains an author object that can be used to access the various different data fields associated with authors. The following snippet will print out the authors name, profile and homepage, if this information is not filled in then nothing will be printed.

  1. <h2><?php echo $curauth->first_name . ' ' . $curauth->last_name; ?> (<a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a>)</h2>
  2. <p><?php echo $curauth->user_description; ?></p>

You can also include a normal loop on this page to print out every post that the user has written.

For more information on the author templates (and a full list on the attributes available in the author object) take a look at the author templates section on the Wordpress site.

Comments

Permalink
thanks for the code!!

WindStarCasino (Fri, 02/27/2009 - 14:44)

Add new comment

The content of this field is kept private and will not be shown publicly.