admin管理员组

文章数量:1127070

I was asking if is possible to get all images from Media Library and display one of them randomly every time I refresh the page.

I think it'd be something like make an array of media library images (php) and then just call one of them trying not to request unnecessary requests

The think is I'm new in this world and I don't know how to start. Can anyone help me?

Thanks!

I was asking if is possible to get all images from Media Library and display one of them randomly every time I refresh the page.

I think it'd be something like make an array of media library images (php) and then just call one of them trying not to request unnecessary requests

The think is I'm new in this world and I don't know how to start. Can anyone help me?

Thanks!

Share Improve this question asked May 8, 2020 at 14:57 NewbieNewbie 11 bronze badge 3
  • Many hosting providers don't allow random sorting as it's so memory intensive. Not to mention that if you have a big media library, the experience could be slow. – rudtek Commented May 8, 2020 at 15:12
  • but you can list them and not to call them to not generate unnecessary requests – Newbie Commented May 8, 2020 at 15:26
  • the "php" which you mentioned is would be RAND(). Many hosts don't allow that function to run. – rudtek Commented May 8, 2020 at 15:46
Add a comment  | 

2 Answers 2

Reset to default 1

get_posts to retrieve a random attachment from the Media Library based on the orderby parameter set to rand (random)

// Get a random image from the Media Library
$args = array(
    'post_type' => 'attachment',
    'numberposts' => 1, // 1 means need single image 
    'orderby' => 'rand',
    'post_status' => 'inherit',
);

$random_single_image = get_posts($args);

if ($random_single_image) {
    foreach ($random_single_image as $image) {
        // Display the image
        echo wp_get_attachment_image($image->ID, 'full'); // Change 'full' to your preferred image size
    }
} else {
    echo 'No images found.';
}

I can't comment due to lack of reputation, so I'll address the issue rudtek mentioned. Personally, I never came across a host that disables PHP's rand() function, but in case there is such a lousy host, you can opt for a more efficient way of doing things. What you can do is to get all the image ids, and select a random id from the available list. This way you greatly reduce the memory footprint and cpu usage. See the code sample below

// get all image ids available
$image_ids = get_posts( 
    array(
        'post_type'      => 'attachment', 
        'post_mime_type' => 'image', 
        'post_status'    => 'inherit', 
        'posts_per_page' => -1,
        'fields'         => 'ids',
    ) 
);
// based on the number of image_ids retrieved, generate a random number within bounds.
$num_of_images = count($image_ids);
$random_index = rand(0, $num_of_images);
$random_image_id = $image_ids[$random_index];
// now that we have a random_image_id, lets fetch the image itself.
$image = get_post($random_image_id);

// you can do whatever you want with $image now.

The code above shouldn't have any issues, but you will have to work on it further if you wish to display the random image chosen. If you don't have to necessary programming skill to get it working, I might try to write out a low-footprint solution sometime soon.

Alternatively, there should be plugins available to do what you want, just that they may indeed be quite bloated. Also note that performance might be heavily affected if you have thousands or more images in your library.

本文标签: phpDisplay one random image from Media Library