2

The following lists authors of the current category.
But I want this to be a list of 6 random authors from current post category.

foreach((get_the_category()) as $category) {
  $postcat= $category->cat_ID;
 }

$current_category_ID = $postcat;
$current_cat_id = $current_category_ID;
$author_array = array();
$args = array(
'numberposts' => -1,
'cat' => $current_cat_id,
'orderby' => 'author',
'order' => 'asc',

);
$cat_posts = get_posts($args);
foreach ($cat_posts as $cat_post) :
if (!in_array($cat_post->post_author,$author_array)) {
$author_array[] = $cat_post->post_author;
}
endforeach;
foreach ($author_array as $author) :
$auth = get_userdata($author)->display_name;
$auth_link = get_userdata($author)->user_login;
$autid= get_userdata($author)->ID;
$link = get_author_posts_url($autid);
echo ''. get_avatar( $autid, '46' ).'';
echo "<a class='sidebar-aut' href='$link";
echo "'>";
echo '<h6>'.$auth.'</h6>';
echo "</a>";
echo "<div class='clearfix'></div>";
echo "<br />";
endforeach;
4
  • Can, did I interpret it right: "list of 6 random authors"? I'm not quite sure because the code you're trying $category_detail=get_the_category($post->ID) doesn't make much sense... Maybe what you need are PHP functions: suffle and array_slice.
    – brasofilo
    Commented Jan 30, 2013 at 22:45
  • I update my code, im listing right now, just i need random 6 or any number list author.
    – Genxer
    Commented Jan 30, 2013 at 22:51
  • I forgot to comment: please, add indentation to your code, it's easier to read, for you and for us. ::::: Did you look at the PHP functions?
    – brasofilo
    Commented Jan 30, 2013 at 22:57
  • I will pay attention to it..Yes but i never used it before and insufficient knowledge of my php. 'shuffle($author_array)' this do random but how can i list any number author(e.g just 6 author) :)
    – Genxer
    Commented Jan 30, 2013 at 23:30

1 Answer 1

0

After you populate $author_array, you need to pick 6 random users out of it. Here are two straightforward choices:

  1. Shuffle the array shuffle($author_array); and pop values off of it in a for or while lopp using array_pop()
  2. Create a randomized copy of six elements of the array using array_rand($author_array,6); and then iterate through the new randomized array using foreach as you did above.

Personally I prefer #2, but please note that if you try to pick 6 random elements from an array of fewer than 6 elements with array_rand() you'll get a warning. You'd want to test the size of $author_array with count($author_array) first to either limit your array_rand() command to the max size of the potential list of authors, or to skip it entirely if it's 1.

Something like, after your first foreach ends:

if(count($author_array) >= 6) {
    $max = 6;
} else {
    $max = count($author_array);
}

$random_authors = array_rand($author_array,$max);
foreach ($random_authors as $author) { ...
1
  • Thanks for your answer. I did try with your code, but same author listing always and looped didnt stop. I did try with the following code and i want to share with maybe you can advise me. Before shuffle and later call first 5 element. Code: 'shuffle($author_array); $author_rnd= array_slice ($author_array , 0, 5);'
    – Genxer
    Commented Jan 31, 2013 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.