Displaying All Users Information

WordPress provides an archive template that displays all posts, but there is no template that displays a list of all users. Let's say that you are building a business site, and you want to take advantage of the new contact fields you defined above to display a user directory that will automatically reflect any changes your users make to their contact information. To do so, create a new page template containing the code shown in Listing 10-5. This code should be placed more or less where the_content() would usually go; you'll need to add the surrounding markup to match your site's theme.

In this example, you get all user IDs of your blog with the get_users_of_blog() function. Then, to make it easier to loop through them, you go through all the user objects returned by that function and create a simple array. Finally, you loop through the array and display each field in a table cell.

Notice that the object field names for the new contact fields match the short names you used when you created them.

Figure 10-5 shows the resulting directory page.

Listing 10-5. Building a user directory

Template Name: Staff Directory

//get all registered users $blogusers = get_users_of_blog(); foreach ($blogusers as $bloguser) {

//get data about each user as an object $user = get_userdata($bloguser->user_id); // create a flat array with only the fields we need $allusers[$user->ID] = array(

'name' => $user->display_name, 'title' => $user->title, 'phone' => $user->phone, 'twitter' => $user->twitter

<table id="staff-directory" class="sortable"> <thead>

<tr> <th>Name</th> <th>Title</th> <th>Phone</th> <th>Twitter</th> </tr>

<?php foreach ($allusers as $auser) { ?> <tr>

<td><?php echo $auser['name']; ?></td> <td><?php echo $auser['title']; ?></td> <td><?php echo $auser['phone']; ?></td>

<td><a href="http://twitter.com/<?php echo $auser['twitter']; ?>"> @<?php echo $auser['twitter']; ?></a></td>

</tr> <?php } ?> </tbody> </table>

Figure 10-5. The resulting Staff Directory

See sillybean.net/?p=2715 for a more complex version of this user directory, including a method of creating downloadable vCard address book files for each user using microformats.

Was this article helpful?

0 0
The Tweet Success Guide

The Tweet Success Guide

Increase Your Sales by Understanding How to Effectively Market on Twitter. Social Network Sites Such as Twitter are One of the Major Marketing Venues for Successful Promotion of Your Business, Products andor Services, but Effective Strategy is Crucial to Achieve Your Objectives.

Get My Free Ebook


Post a comment