我正在使用下面的代码,在我的wordpress网页上显示所有作者的博客头像。然而,我想展示10个随机的头像,而不是所有的。有没有什么方法可以不用插件呢?
$blogusers = get_users_of_blog();
if ($blogusers) {
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->user_id);
echo get_avatar( $user->ID, 70 );
}
}发布于 2015-06-01 09:04:56
get_users_of_blog已弃用。请改用get_users:
https://codex.wordpress.org/Function_Reference/get_users
单独使用get_users不能获得随机用户,但可以使用pre_user_query操作更新ORDER BY子句:
// Update the query to order by random
function randomize_users(&$query) {
$query->query_orderby = "ORDER BY RAND()";
}
// Attach the function to the pre_user_query hook
add_action( 'pre_user_query', 'randomize_users' );
// Get the users
$users = get_users(array(
'blog_id' => get_current_blog_id(),
'number' => 10
));
// Remove the action so future queries won't be affected
remove_action( 'pre_user_query', 'randomize_users' );https://stackoverflow.com/questions/30563872
复制相似问题