I’m trying to loop througt all images of a certain folder with the following script:
function loopImages(){
$imagePath = bloginfo('template_url') . '/assets/images/' . 'casa_intera';
echo $imagePath;
$fileList = list_files( $imagePath, 2 );
foreach ( $fileList as $file ) {
echo '<img src="' . $imagePath . $file . '" alt="" data-fancybox="gallery" loading="lazy">';
}
}
loopImages();
It only echoes the $imagePath
as http://localhost:8888/wordpress/wp-content/themes/casale-wp/assets/images/casa_intera
which is correct, but doesn’t output the rest.
Thanks
EDIT
currently I have this code:
function loopImages(){
$imagePath = bloginfo('template_url') . '/assets/images/' . 'casa_intera';
$fileList = list_files( $imagePath, 2 );
foreach ( $fileList as $file ) {
var_dump( $file );
}
}
loopImages();
But I only get this back as an HTML item
" http://localhost:8888/wordpress/wp-content/themes/casale-wp"
2
Answers
At the end I went with this solution.
Here is an option using
glob
to get all jpeg files in a directory. Thelist_files
function is an admin function and will not work on the front end.