There is a working code (checked on pure php), everything works, images are randomly output from the directory, but I try to output it in the wordpress template - it does not output anything. Could you tell? Here is the code

$root = ''; $path = 'images/'; function getImagesFromDir($path) { $images = array(); if ( $img_dir = @opendir($path) ) { while ( false !== ($img_file = readdir($img_dir)) ) { // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } function getRandomFromArray($ar) { mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed $num = array_rand($ar); return $ar[$num]; } // Obtain list of images from directory $imgList = getImagesFromDir($root . $path); $img = getRandomFromArray($imgList); 
  • $path = '/images/'; - if the script is NOT in the root of the site, specify the path to the folder relative to the root of the site - Blacknife
  • I tried it - it does not help. I also tried to write the full path - site.com .... I suppose you have to write something in ROOT. ... Tried it in ROOT $ _SERVER ['DOCUMENT_ROOT'] - user3654202

1 answer 1

Needs to be replaced

 $imgList = getImagesFromDir($root . $path); 

on

 $imgList = getImagesFromDir($root . '/' . $path) 
  • Made. The file has been created. Here is my path $ path = 'wp-content / themes / generic / images /'; But the picture is not displayed. I have nothing included. All I write in the file header.php and the function and its call - user3654202
  • I do not understand, the file is being created ... - user3654202
  • Yes, data.txt is created in the images folder. I tried var_dump ($ img_file); - It's funny, but displays nothing, it's generally empty .... Damn as it is. I use the generic wordpress theme - user3654202
  • Added by. Here’s what the string (33) "wp-content / themes / generic / images /" string (50) "Z: / home / site1 / wwwwp-content / themes / images /" - user3654202
  • Look, please, here is my var_dump($path); function getImagesFromDir($path) { $images = array(); var_dump($path); if ( $img_dir = @opendir($path) ) { var_dump($path); while ( false !== ($img_file = readdir($img_dir)) ) { var_dump($img_file); // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } code var_dump($path); function getImagesFromDir($path) { $images = array(); var_dump($path); if ( $img_dir = @opendir($path) ) { var_dump($path); while ( false !== ($img_file = readdir($img_dir)) ) { var_dump($img_file); // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } var_dump($path); function getImagesFromDir($path) { $images = array(); var_dump($path); if ( $img_dir = @opendir($path) ) { var_dump($path); while ( false !== ($img_file = readdir($img_dir)) ) { var_dump($img_file); // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } var_dump($path); function getImagesFromDir($path) { $images = array(); var_dump($path); if ( $img_dir = @opendir($path) ) { var_dump($path); while ( false !== ($img_file = readdir($img_dir)) ) { var_dump($img_file); // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } - user3654202