Use correct path to display latest uploaded images

25 views Asked by At

I built a gallery script on my own and on my wordpress blog I successfully displayed 12 images that are uploading through various sub-directories of that gallery.
However, when checking the single post page, the code fails because instead of linking to https://your-domain.com/gallery/... it links to the post permalink+/gallery/.....

This is the code I am using (from https://stackoverflow.com/a/34511956/23556208)

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./gallery"));
    $rgIt = new RegexIterator($it, "/^.+\.jpg$/i");

    $files = iterator_to_array($rgIt);

    usort($files, function($a, $b){
      if(filectime($a) == filectime($b))
        return 0;
      return filectime($a) > filectime($b) ? -1 : 1;
    });

    $files = array_slice($files, 0 , 12);

    foreach($files as $v){
        echo "<a href='".$v."' target='_blank'><img src='". $v ."' /></a>";
    }

How can I implement the URL so on the single page they go to that https://your-domain.com/gallery exclusively and not to the post permalink first?

1

There are 1 answers

0
Lady C On

The fixes to correct on my code were:

  1. removing the ./ from RecursiveDirectoryIterator

    new RecursiveDirectoryIterator('gallery')

  2. Add a / for both link and image

    <a href='/". $v ."' target='_blank'><img src='/". $v ."' /></a>