Skip to main content
deleted 241 characters in body
Source Link
Genxer
  • 195
  • 1
  • 11

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

function getCurrentCatIDforeach(){
    global $wp_query;

    if( is_categoryget_the_category()) ||as is_single($category) ){
        $cat_ID =$postcat= get_query_var('cat');$category->cat_ID;
    }
 
    return $cat_ID;
}

$current_category_ID = getCurrentCatID();$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;

I tried with this code but it didn't work:

  global $post;
  $category_detail=get_the_category( $post->ID );

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

function getCurrentCatID(){
    global $wp_query;

    if( is_category() || is_single() ){
        $cat_ID = get_query_var('cat');
    }
 
    return $cat_ID;
}

$current_category_ID = getCurrentCatID();
$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;

I tried with this code but it didn't work:

  global $post;
  $category_detail=get_the_category( $post->ID );

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;
Text interpretation. Code indentation.
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

How can iI list random authorauthors from current post category?

My code doing , listingThe following lists authors of the current category. 
But iI want this to be a list randomof 6 authorrandom authors from current post category.

My Code:

<?php

function getCurrentCatID(){
 
    global $wp_query;

    if( is_category() || is_single() ){
 
        $cat_ID = get_query_var('cat');
    }

    return $cat_ID;
 
}

$current_category_ID = getCurrentCatID();
$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;
?>

I did trytried with this code but it's not workedit didn't work:

  global $post;
    $category_detail=get_the_category( $post->ID );

How can i list random author from current post category?

My code doing , listing authors of the current category. But i want this list random 6 author from current post category.

My Code:

<?php

function getCurrentCatID(){
 
global $wp_query;

if(is_category() || is_single()){
 
$cat_ID = get_query_var('cat');
}

return $cat_ID;
 
}

$current_category_ID = getCurrentCatID();
$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;
?>

I did try with this code but it's not worked:

  global $post;
    $category_detail=get_the_category( $post->ID );

How can I list random authors from current post category?

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

function getCurrentCatID(){
    global $wp_query;

    if( is_category() || is_single() ){
        $cat_ID = get_query_var('cat');
    }

    return $cat_ID;
}

$current_category_ID = getCurrentCatID();
$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;

I tried with this code but it didn't work:

  global $post;
  $category_detail=get_the_category( $post->ID );
Source Link
Genxer
  • 195
  • 1
  • 11

How can i list random author from current post category?

My code doing , listing authors of the current category. But i want this list random 6 author from current post category.

My Code:

<?php

function getCurrentCatID(){

global $wp_query;

if(is_category() || is_single()){

$cat_ID = get_query_var('cat');
}

return $cat_ID;

}

$current_category_ID = getCurrentCatID();
$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;
?>

I did try with this code but it's not worked:

  global $post;
    $category_detail=get_the_category( $post->ID );