(Sorry if the title is pretty useless)
I have this function to get the first image from a random post in WordPress. This works great, but now I need it to select a random image from all the matches, rather than the first. (I'm running this function in a query_posts loop to select the categories)
// Get first image in post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
//no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
// Or, show first image.
return $first_img;
}
So, any ideas, links, tips & tricks on how to select a random result from the matches results?