1

i'm building a wordpress theme and i'm trying to get the all the jpg files from a directory in random order using php.... im using Xampp on win7 (localhost).

this is the code:

<? 
    $dir =  get_template_directory_uri().'/images/top/';

    $file_display = array ('jpg', 'jpeg');
    if(file_exists($dir) == false){
        echo 'Directory \'', $dir, '\' not found';
    } else {
       $dir_contents = scandir($dir);
       shuffle($dir_contents);
       foreach ($dir_contents as $file) {
           $file_type = strtolower(end(explode('.', $file)));
           if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true){ 
                echo '<img  src="', $dir, '/', $file, '" alt="', $file, '" />';    
           }
       }
   } 
?>

i always get

Directory 'http://localhost/ni/wp-content/themes/A/images/top/' not found 

i also tried to change

$dir =  get_template_directory_uri().'/images/top/';

to:

    $dir = "C:\xampp\htdocs\Ni\wp-content\themes\A\images\top\";

but still no luck, any help would be appreciated!

7
  • 2
    Do you have and if in front in front of the (file_exists($dir) == false) test? Commented May 2, 2013 at 8:55
  • Yes i edited the question, thank you! i had it on my original code! Commented May 2, 2013 at 8:57
  • try replacing get_template_directory_uri by get_template_directory, because the get_template_directory_uri gives an URI (and you don't want that) Commented May 2, 2013 at 8:59
  • @EaterOfCorpses As of PHP 5.0 file_exists accepts HTTP (amongst a few other protocols) so it shouldn't matter so long as OP is using PHP5. Commented May 2, 2013 at 9:04
  • using get_template_directory() outputs: C:\xampp\htdocs\Ni/wp-content/themes/A/images/top/ with mixed slashes (linux windows syle?) and i get Directory '' not found Commented May 2, 2013 at 9:04

1 Answer 1

1

This is how i made it work.

  <? 
            $dir =  get_template_directory().'/images/top';
            $imageDir= get_template_directory_uri().'/images/top';
            $file_display = array ('jpg', 'jpeg');
            if (file_exists($dir) == false) {
              echo 'Directory \'', $dir, '\' not found';
            } else {
              $dir_contents = scandir($dir);
              shuffle($dir_contents);
              foreach ($dir_contents as $file) {
                $file_type = strtolower(end(explode('.', $file)));
                if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
                  echo '<img src="', $imageDir, '/', $file, '"  />';
                }
              }
            } 
  ?>
Sign up to request clarification or add additional context in comments.

2 Comments

by using get_template_directory() to get the array. and get_template_directory_uri() to show the images. thank you all for helping me out to get to this...
Note that in most cases it's better to use get_stylesheet_directory() rather than get_template_directory() if you're trying to load assets because if you're using a child theme the former will return the child theme's directory, whereas the latter will return the parent theme's directory, and will thus cause you to miss child-theme assets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.