I’m trying to utilize the authors from wordpress to create a spotlight block in the sidebar that selects 1 random author from multiple users and only displays that 1 users information. I want to include the author’s display name, a thumbnail, snippet of their bio and a link to the full author page. I haven’t found any plugins that support this. I’m not a PHP/MySQL wiz so custom writing a function is a bit outside my abilities however I can read it and follow the code. What I have so far is:
<?php
global $wpdb;
$ids = $wpdb->get_results( "SELECT {$wpdb->usermeta}.user_id from {$wpdb->usermeta} where ({$wpdb->usermeta}.meta_key='wp_capabilities') and ({$wpdb->usermeta}.meta_value >0 )" );
$user_id = rand(1,count($ids));
$wpdb->show_errors();
?>
<h2><?php get_the_author_meta( 'display_name', $user_id ); ?></h2>
<img class="left" src="" alt="<?php print $selected_user; ?>" />
<p><?php get_the_author_meta( 'user_description', $user_id ); ?> <a href="<?php get_the_author_meta( 'user_url', $user_id ); ?>">read more</a>
This returns an array but I am unsure of what to do to get the output I need. Thanks in advance.
You can cut a lot of steps out of this if you use
get_users()
instead of your custom SQL query. You can then select a random user out of that array usingarray_rand()
(native PHP function, not a wordpress function) and it will return the key you should be using. Here’s an example:This will leave you with
$user
, which will contain a user object with just about everything you could want on a user…or at least the ability to obtain what you need.get_avatar()
will do the thumbnail…assuming you want the default.