The function is to display a randon image from the folder 2021/01 Since the time is changing every month, how to change it in the first line? To make 2021/01 display the current year and month? For example, next month it will change to <?php $randomdir = dir(‘wp-content/uploads/2021/02’);
<?php $randomdir = dir('wp-content/uploads/2021/01'); $count = 1; $pattern="/(gif|jpg|jpeg|png)/"; while($file = $randomdir->read()) { $ext = pathinfo($file, PATHINFO_EXTENSION); if (preg_match($pattern, $ext)) { $imagearray[$count] = $file; $count++; } } $random = mt_rand(1, $count - 1); echo '<img src="wp-content/uploads/2021/01/'.$imagearray[$random].'" alt />'; ?>
Advertisement
Answer
Use date
function for the same.
$randomdir = dir('wp-content/uploads/' . date('Y/m'));