Im列出所有作者列表的当前类别,但我想列出当前类别中的随机作者列表。
我尝试了“order”=>“RAND”,RAND代码不起作用。
我的代码:
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\',
\'posts_per_page\' => \'10\',
);
$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;
最合适的回答,由SO网友:s_ha_dum 整理而成
The Codex lists rand
as the orderby
string, 不RAND
. 我怀疑这两种方法都有效,但我还没有检查。但我不会这样做。我倾向于像。。。
$current_category_ID = getCurrentCatID();
$current_cat_id = $current_category_ID;
$author_array = array();
$args = array(
\'numberposts\' => -1,
\'cat\' => $current_cat_id
);
$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;
shuffle($author_array); // randomize
数据库
ORDER BY
效率不太高。
ORDER BY RAND
效率特别低。该代码在较小的数组上用PHP进行排序,因此它的性能应该更好。