How to fetch all uploaded images

Post Reply
koljaxyz
Ssr. Bludit
Posts: 12
Joined: Thu Apr 30, 2020 10:24 am

Hello there,

in the backend, i uploaded a bunch of images. Now i want to render these in my template inside a foreach loop. I do not want to place the images inside the text editor, because i want text and images separately. I know i can get the cover image with

Code: Select all

<?php echo $page->coverImage() ?>
But is there a way to get all the images? Something like:

Code: Select all

<?php foreach ($images as $image): ?>
   <img src="<?= echo $image->url() ?>">
<?php endforeach ?>
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

It's not so easy because the images are not stored in a database file ($page->coverImage() gets the information from the database file pages.php).

Therefore a script first has to add the names of the files of the images in the directory with the images of a page to an array. Then the function foreach can be used.
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
User avatar
novafacile
Master Bludit
Posts: 107
Joined: Sat Oct 06, 2018 4:47 pm
Has thanked: 35 times
Been thanked: 10 times
Contact:

Maybe the ImageGallery plugin could be a solution for you. There you can manage images in albums and add the albums in the end of the page, without adding them in the text editor.
nogajun
Jr. Bludit
Posts: 6
Joined: Sun Apr 05, 2020 4:08 am
Location: Japan

I had the same problem.
I figured out that I needed to get a file list, so I wrote some simple code.

Code: Select all

<?php
// set upload path
$upload_path = PATH_UPLOADS_PAGES.$page->uuid()."/";

// fetch image list
$image_list = Filesystem::listFiles($upload_path, '*', '*');

// output img tag
foreach($image_list as $l):
  echo "<img src=\"".HTML_PATH_UPLOADS_PAGES.$page->uuid()."/".basename($l)."\">\n";
endforeach;
?>
koljaxyz
Ssr. Bludit
Posts: 12
Joined: Thu Apr 30, 2020 10:24 am

nogajun wrote: Fri Mar 04, 2022 5:49 am I had the same problem.
I figured out that I needed to get a file list, so I wrote some simple code.
Nice! This worked, thank you! ♥
Post Reply