dsebao
4/10/2014 - 4:33 AM

Show the first image of every folders finding in a root dir in WordPress

Show the first image of every folders finding in a root dir in WordPress

<?php
/*
In this example bfithumb (https://github.com/bfintal/bfi_thumb) is 
include in functions.php for the thumbnails

The folders have the id gallery, name of the gallery and the date in his name.
eg: 123_mytitle_02.12.2014
*/

$search = dirname(__FILE__) . "/{foldername}/";
$ffs = scandir($search);

foreach($ffs as $ff){
  //i don´t want this foolders
  if($ff != '.' && $ff != '..' && $ff !== ".DS_Store"){
    if(is_dir($search.'/'.$ff)){
      //save folders in an array
      $folders[] = $ff;
    }
  }
}

//Create the loop for every folders finded
for ($i = 0; $i < sizeof($folders); $i++){
  $data = explode("_",$folders[$i]);
  $files = glob($search . $folders[$i] . '/*.jpg');
  if(!empty($files)){
    sort($files);
    $url = get_bloginfo('template_url') . '/{foldername}/' . $folders[$i] . '/';
    $filename = basename($files[0]);
    $imgurl = $url . $filename;
    $params = array( 'width' => 80,'height' => 80 );?>

    <li>
      <h1><?php echo $data[1];?></h1>
      <a href="?gallery=<?php echo $folders[$i];?>"><img src="<?php echo bfi_thumb( $imgurl, $params )?>" alt=""></a>
      <h3><?php echo $data[2];?></h3>
    </li>
    <?php 
    }
}
?>