Meta Box
Support › General › rwmb_the_value questionResolved
Hi,
how can I use the 'class' => $random_class in the following code, at the moment this does not work
$author_users = get_users( array( 'role' => 'author', 'orderby' => 'rand' ) ); $classes = ['avatar--m', 'avatar--l', 'avatar--xl', 'avatar--xxl']; foreach ( $author_users as $user ) { $random_class = $classes[array_rand($classes)]; rwmb_the_value( 'custom_avatar', [ 'object_type' => 'user', 'size' => 'thumbnail', 'class' => $random_class ], $user->ID ); echo $random_class; }
Oops, I forgot to explain , what I want to achieve, I want to display the thumbnails of all authors, every thumbnail should get a different random class, what's the best way to achieve this ? using the code above ?
Found the solution, so just in case somebody needs the same
<?php // Get all users with the 'author' role $author_users = get_users( array( 'role' => 'author' ) ); // Classes to be randomized $classes = ['avatar--m', 'avatar--l', 'avatar--xl', 'avatar--xxl']; // Randomize the order of authors shuffle($author_users); // Display up to 7 authors $count = 0; foreach ($author_users as $author) { // Displaying uploaded image $image = rwmb_meta('custom_avatar', ['object_type' => 'user', 'size' => 'thumbnail'], $author->ID); // Check if the user has a custom avatar image if (!empty($image)) { $random_class = $classes[array_rand($classes)]; echo '<img src="' . $image['url'] . '" class="' . $random_class . '">'; $count++; } if ($count >= 7) { break; // Stop after displaying 7 authors } } ?>