Skip to content

Instantly share code, notes, and snippets.

@kythin
Created June 22, 2012 00:52
Show Gist options
  • Save kythin/2969603 to your computer and use it in GitHub Desktop.
Save kythin/2969603 to your computer and use it in GitHub Desktop.
Grabs a directory of images from the wp uploads folder and displays them, also using their filenames for ordering and alts/titles.
<?php
/* Chris' magical logo display code.
* This will display all images in the uploads/logos directory, and use their filenames for the alt's and titles.
* Also allows file ordering by using numbers at the start of the filename, optionally followed by an underscore.
* Hyphens will be converted to spaces in the alt and title. Make sure to use the right case in the filename as you want
* in the alt & title.
* e.g. 0_Company.php => Company
* 05_Company-Name => Company Name
* 05_logo-Company-Name => Company Name
*/
$uploads = wp_upload_dir();
$directory = $uploads['basedir']."/logos/";
$images = glob($directory . "*.png");
asort($images);
foreach($images as $image)
{
$filename = explode('/', $image);
$filename = $filename[count($filename)-1];
$filepath = $uploads['baseurl']."/logos/$filename";
$alt = str_replace('.png','',$filename);
$alt = str_replace('logo-','',$alt);
//check if the first char is a digit, if so kill it because it was only there to order the array sort.
while (is_numeric(substr($alt, 0, 1))){
$alt = substr($alt, 1);
}
//in case it's got an underscore after the ordering number e.g. 0_
if (substr($alt, 0, 1) == '_'){
$alt = substr($alt, 1);
}
$alt = str_replace('-',' ',$alt);
echo ("<img class='alignnone size-full' style='width: auto;' title='$alt' src='$filepath' alt='$alt' />
");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment