• I’ve managed to cobble together some code that parses a folder and displays all images located in that folder. My problem is that I need to attach a css class (class=”last”) to the very last image in the loop.

    Any help much appreciated (by lots of folks on my mailing list)…

    Here’s the code I have so far.

    function get_images() {
    
    	// only find files with these extensions
    	$exts = 'jpg jpeg png gif';
    
    	$str = ''; $i = -1; // Initialize some variables
    	$folder = './wp-content/uploads';
    
    	$handle = opendir($folder);
    	$exts = explode(' ', $exts);
    	while (false !== ($file = readdir($handle))) {
    		foreach($exts as $ext) { // for each extension check the extension
    			if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
    				//$str .= $file;
    				$str .="<img src='wp-content/uploads/". $file ."' alt='" . $file . "' />";
    				//if ($str) $str .= '|';
    				++$i;
    			}
    		}
    	}
    	echo $str;
    	closedir($handle); // Were not using it anymore
    	return $str;
    }
  • The topic ‘How to determine the last element in a loop’ is closed to new replies.