• Aucun résultat trouvé

Displaying author avatars and descriptions

Dans le document WordPress 2.8 Themes Cookbook (Page 148-153)

Multi-author blogs are gaining momentum in the professional and business blogging world. As such, if you're producing a business theme for WordPress, you might want to take special care to produce a page template that displays information about each of the authors on a blog.

In this recipe, you'll create such as page template. This will show the author's display name, avatar, biography, and the number of posts that they've written for the site. It will also contain a link to that author's posts.

Getting ready

To properly use the techniques in this recipe, you'll need to be working with a theme that you previously acquired or developed. If you haven't started developing a custom theme yet, I recommend using the Thematic theme. It can be freely downloaded from the WordPress.org Theme Repository at http://wordpress.org/extend/themes/thematic/.

How to do it...

First, follow the steps in the recipe Creating a simple page template until you reach the point at which you start adding custom content. While following that recipe, modify the filename from hello-world-page-template.php to authors-page-template.php, and change the value of the Template Name: header from Hello World to Authors. Now you're ready to start adding the appropriate content. After the page template comment header, add the following markup to your page template, and then save the file:

<?php get_header(); ?>

<div id="container">

<div id="content">

<?php

$authors = get_users_of_blog();

foreach($authors as $author) {

$num_posts = get_usernumposts($author->ID);

if($num_posts>0) {

<div class="author" id="author-<?php echo $id; ?>">

You can now use your Authors page template. Go and create a new page in the WordPress administrative area, and make sure that it uses the Authors page template. If you need more information on how to do this, see the recipe Creating a simple page template. Visit your newly-created page. You should see output similar to the following, depending on the authors that you have on your site:

In the above screenshot, you see numerous authors with their name, description, avatar, and a link to their posts. The information could be expanded upon, but this is a good starting point.

How it works...

By this point you should have a pretty good idea of the way that custom page templates work. If you need a refresher, see the How it works... section of the Creating a simple page template recipe.

There are a few functions of note in this recipe, nearly all of them dealing with the retrieval of author data. The code listing starts with the get_users_of_blog function. This function returns an array of user objects, one for each user currently in the system. Next, you iterate over the array of users, checking to see whether they have published any posts or not. If an author has published at least one post, then you proceed with displaying various user data.

Here, user data is displayed by using the the_author_meta function with different parameters and the value of the user ID for the author currently being iterated over. In addition, the get_

avatar function is used to display the appropriate image for each author. All of the data for each author is wrapped in a nice set of HTML tags that provide proper formatting and display.

There's more...

If you're going to display information for each author on a dedicated page, you should probably redisplay that information on their individual author listings as well. You already have the proper markup, so this will be a piece of cake.

First, separate out the display code for an author into its own file. You could call this file something like author-expanded.php. It will contain the following code:

<div class="author" id="author-<?php echo $id; ?>">

<h2 class="author-name">

echo get_avatar($id);

?>

<a href="<?php echo get_author_posts_url($id); ?>">

<?php printf(

'%s has written %d posts. Check \'em out!', get_the_author_meta('display_name',$id), $num_posts

Now go back to your authors-page-template.php, and change it to use the newly-created file, leaving you with something like the following:

<div id="container">

<div id="content">

<?php

$authors = get_users_of_blog();

foreach($authors as $author) {

$num_posts = get_usernumposts($author->ID);

if($num_posts>0) { $id = $author->ID;

$author = new WP_User($id);

include(STYLESHEETPATH.'/author-expanded.php');

} } ?>

</div>

</div>

After that, open up your theme's author.php file (if you don't have one, just create one and copy the contents of index.php into the new file). Immediately before the posts listing, insert a call for the expanded author information. Your code should look something like the following example:

<?php

global $wp_query;

$id = $wp_query->get_queried_object_id();

$author = new WP_User($id);

$num_posts = get_usernumposts($id);

include(STYLESHEETPATH.'/author-expanded.php');

if(have_posts()) { while(have_posts()) { the_post();

Call up an author's post page, and you'll see the expanded author information, followed by a list of that user's posts:

See also

Creating a simple page template

Dans le document WordPress 2.8 Themes Cookbook (Page 148-153)