WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display random authors on homepage (16 posts)

  1. Joshuwar
    Member
    Posted 4 years ago #

    Hi there- thanks for reading.

    I'm trying to display two randomly chosen blog authors from a site on its homepage. I'm trying to get them to display as gravatars, and names.

    Unfortunately, my php skills are weak- I've built this, which displays all the authors, fine, but I'm not sure how to make it just choose a random author.

    <?php
    $order = 'user_nicename';
    $user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users ORDER BY $order"); // query users
    foreach($user_ids as $user_id) : // start authors' profile "loop"
    $user = get_userdata($user_id);
    ?>
    <div class="mugshot">
    <?php
    echo '<a href="' . $user->yim . '">';
    echo get_avatar(  $user->user_email, '60', 'http://no-image.jpg' );
    echo '<br />' . $user->nickname . '</a>';
    ?>
    </div>
    <?php
    endforeach; // end of authors' profile 'loop'
    ?>

    (I'm using the $user->yim (yahoo IM) field to contain the address of the author page, so users can click through from the homepage)

    If anybody can help me with a push in the right direction, or if people have done similar things in the past- I'd be really happy.

    Thanks again!

  2. Joshuwar
    Member
    Posted 4 years ago #

    Well, with some tinkering (and some help from this chap) I've got it doing what I want, but it's a bit of a hatchet job.

    In case anybody wants to do a similar thing, here's my code. I'd also welcome any assistance/suggestions for cleaning it up- I'm sure it's a rather messy way of doing it.

    (left all my echoes in for testing)

    <?php
    $order = 'user_nicename';
    $user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users ORDER BY $order"); // query users
    $n = $user_ids;
    echo count($n);
    $id1 = rand(1,count($n));
    echo 'Random ID:' . $id1;
    $id2 = rand(1,count($n));
    echo '<br />Random ID2:' . $id2;
    while ($id1 == $id2) {
    $id2 = rand(1,count($n));
    break;
    }
    echo '<br />While ID: ' . $id2;
    $user = get_userdata($id1);
    
    ?>
    <div class="mugshot">
    	<?php
    	echo $user_id;
    	echo '<a href="' . $user->yim . '">';
    	echo get_avatar(  $user->user_email, '60', 'http://noimage.jpg' );
    	echo '<br />' . $user->nickname . '</a>';
    	?>
    </div>
    <?php $user = get_userdata($id2);
    
    ?>
    <div class="mugshot">
    	<?php
    	echo $user_id;
    	echo '<a href="' . $user->yim . '">';
    	echo get_avatar(  $user->user_email, '60', 'http://noimage.jpg' );
    	echo '<br />' . $user->nickname . '</a>';
    	?>
  3. Joshuwar
    Member
    Posted 4 years ago #

    There's a problem with this- if you delete a user, it screws up, because the count of the array no longer matches up with the author ids.

    For example, if you have 5 users, with ids from 1 to 5, it works fine. But then if you delete one and add another, the new author's id is set to 6, even though there are still only 5 authors. This means that author's id will never be selected, and instead a blank profile is featured.

    I've worked around this by fiddling the id row in the database, but its not an ideal solution, because you have to do it for every subsequent author added.

    If anybody can think of a different way of doing this, or knows how to reset the author id creation, so that ids are automatically reused if the author is deleted, I'd be really pleased to hear of it.

    Cheers,

    J

  4. WhitePhantom
    Member
    Posted 4 years ago #

    Hi JoshuWar,

    Sorry, I can't help you with what you're doing, but I'm interested in learning more about the code you're using.

    I want to read records from a db to display them on a WP website, and your code "SELECT ID FROM $wpdb->users" caught my attention...

    Please excuse my complete and utter ignorance of WP, but the db you're reading from - where did that db come from? did you create it within WP or what?

    Sorry for a really dumb question!

    J.

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    Here try this instead...

    <?php
    $user_ids = $wpdb->get_col("
    	SELECT ID FROM $wpdb->users
    	WHERE user_status = 0");
    shuffle($user_ids);
    array_rand($user_ids);
    
    for($someNumber = 1; $someNumber <= 2; $someNumber++) {
    	$user = get_userdata($user_ids[$someNumber]);
    	?>
    	<div class="mugshot">
    		<a href="<?php echo $user->yim ;?>">
    			<?php echo get_avatar( $user->ID, '60' );?>
    			<br />
    			<?php echo $user->user_nicename ;?>
    		</a>
    	</div>
    	<?php
    }
    unset($user_ids);
    ?>

    I only have 4 user accounts on my test install, but the results were always random and pulled only valid users. Should be a little lighter then your current version..

  6. Joshuwar
    Member
    Posted 4 years ago #

    Hey Telos, thanks a lot for taking the time to do that- yours is a much more elegant solution. Nice site, by the way.

    Maybe you can think of a better way of pulling the link to the author page than using the $user->yim field as I've been doing? It seems a bit hacky.

    And WhitePhantom- I'm using the wpdb class to pull user information from the standard wordpress database- it was created automatically by wordpress when I installed it.

    If you want to pull information from another db, there might be plugins out there which could help you. This thread might be of interest. Hope that helps you.

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    @Joshuwar
    There's actually a slightly better way to do this using the new 2.8 function the_author_meta... *i had a little play with it and checked the codex*..

    You still need to grab user ID's but you're not grabbing "ALL" user data in this case...

    <?php
    $uid = $wpdb->get_col("SELECT ID FROM $wpdb->users WHERE user_status = 0");
    shuffle($uid);
    array_rand($uid);
    
    for($someNumber = 1; $someNumber <= 3; $someNumber++) {
    
    	$userID = $uid[$someNumber];
    	?>
    	<div class="mugshot">
    		<a href="<?php the_author_meta( 'user_url', $userID ) ?>">
    			<?php echo get_avatar( $userID, '60' );?>
    		</a>
    		<br />
    		<!-- <a href="<?php the_author_meta( 'jabber', $userID ) ?>"> -->
    		<!-- <a href="<?php the_author_meta( 'aim', $userID ) ?>"> -->
    		<a href="<?php the_author_meta( 'yim', $userID ) ?>">
    			<?php the_author_meta( 'display_name', $userID ) ?>
    		</a>
    	</div>
    	<?php
    }
    unset($uid,$userID);
    ?>

    Why aren't you using the author/user's URL field instead?... that's what it's for.... (can see that in the above example anyway)...

    See here for additional author meta fields that you can add to the above code...
    http://codex.wordpress.org/Template_Tags/the_author_meta

    This way you're just selecting what you need rather then grabbing all the user data but only showing selective pieces...

    @WhitePhantom
    $wpdb->(

    Refers to a class (think of a class as a glorified set of functions) which interacts with your WordPress database..

  8. Joshuwar
    Member
    Posted 4 years ago #

    Excellent! So I'm guessing that's even faster? Evidently I have a lot to learn.

    I'm using the user's url field as a link to the author's personal site (if applicable) on their profile page- whereas I just want these mugshots to go to that profile page. Hence the yim thing.

    Odd that there isn't a the_author_meta for the author posts page....

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    So what exactly is this profile page, standard WordPress author page, or something custom written?

  10. Joshuwar
    Member
    Posted 4 years ago #

    its an author template- I started from the sample code here, and I've developed it to include the user biog and other information information, whilst the post list is less prominent/bloglike...

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

    So why not use..
    <?php the_author_posts_link(); ?>

    As indicated on the above page... ;)

    It links to the author page... :)

  12. Joshuwar
    Member
    Posted 4 years ago #

    Ha! absurd! Far too easy.

    Brilliant- thanks telos.

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    No problem.. ;)

  14. WhitePhantom
    Member
    Posted 4 years ago #

    Thanks guys. I'm checking out that plugin Joshuwar, hopefully it'll do what I need!

  15. WhitePhantom
    Member
    Posted 4 years ago #

    Guys, that external db mgt plugin - http://wordpress.org/support/topic/222660 - rocks! Many thanks for pointing me in the right direction.

  16. crushlabs
    Member
    Posted 4 years ago #

    Not sure if it matters anymore, but you guys can try this one...

    http://wordpress.org/extend/plugins/author-avatars/

    It pulls random users and you can limit it...

Topic Closed

This topic has been closed to new replies.

About this Topic