« Share This Post in Movable Type 4 | Main | Fighting Hotlinkers with htaccess »

List Random Authors

As communities grow, so does the need to showcase your authors. Having a hefty community of authors is a great thing (content producers FTW!) but everyone wants their fifteen minutes. This handy little tip can help make sure everyone gets their fair share of the limelight.

A couple of things to note first: Make sure that you’ve got author archives set up. You can hack this to work with author profiles, but that’s not the scope of this tutorial. Also, this tutorial assumes that your server is parsing whatever page this is on as PHP. As a rule of thumb, if your file extension is .html, this probably won’t work; if you’re file extension is .php, you should be fine.

<?php
    $displayed_authors = array(); // Will hold indexes from $authors for authors already displayed
    $show = 25; // How many authors should we show?

    <mt:Authors>
        $authors[<mt:AuthorId />] = '<li><a href="<mt:EntryLink archive_type="Author" />"><mt:EntryAuthorDisplayName encode_php='1' /></a></li>';
    </mt:Authors>

    for ($i=1; $i <= $show; $i++) {
        $rn = array_rand($authors); 
        // Loops until it finds an author not displayed
        while(in_array($rn, $displayed_authors)) {
            $rn = array_rand($authors);
        }
        array_push($displayed_authors, $rn);
        echo $authors[$rn];
    }
?>

First, we set up an array where we’ll store all the authors that have already been displayed. More on that in a minute. Then we define how many authors we want to show with this block.

Next, we loop through all the authors, stashing them all into an array. Actually, we’re stashing the html that we want displayed as it gets looped through. This can be basically whatever you want. If you use other Movable Type tags, make sure you add the encode_php='1' filter or else you could get some nasty PHP parse errors and break your site. (Usually because of single quote/double quote mismatching.)

Next up is displaying the authors. We’re going to loop through authors 25 times; remember, that’s what we set up with the $show variable. The array_rand function basically says “pull a random number from 1 to the number of items in our array”. We had to put in a special while loop to make sure it didn’t pick a number already used; this would result in duplicate listings of a single author. The array_push then stores the new, non-duplicate random number in our displayed authors array to ensure we don’t duplicate it next time through the loop.

Finally, we echo a random item from the $authors array. This loops through however many times you’ve told it to with the $show variable.

Note: If you want to keep a particular user from showing up in your random list, there are a few ways to accomplish this. The easiest is to put a <mt:SetVarBlock name="displayname"><mt:AuthorDisplayName /></mt:SetVarBlock> just after the opening <mt:Authors> tag. Then just wrap the code that populates the $authors array in an <mt:Unless> loop, like so: <mt:Unless name="displayname" eq="John Doe"> $authors[<mt:AuthorID />]... </mt:Unless>.

Thanks to Arvind for helping me solve the dupe problem. He also made me say that I will now worship him forever.

Comments (2)

Great tutorial! This is also something easily adaptable to show 25 random entries, comments, tags, categories... very useful.

Yep. The only reason I didn't mention that was because you need to be careful of when using this for other data types is rebuild times. If you're trying to get this to display 20 random entries from the 20,000 in your system, this really isn't the way to go, because you'd have to populate the array with all 20,000 entries just to show 20. Yuck. You'd be better off using lastn="100" to keep rebuild times down.

Post a comment

(If you haven't left a comment here before, your comment may need to be approved before will appear on the entry. Thanks for waiting.)