开发者

Images are loading in random order?

开发者 https://www.devze.com 2022-12-13 23:52 出处:网络
Thanks in advance to anyone who takes time to respond. I truly appreciate this site! I\'ve got a function that loads all images it finds in a given directory (see below). The problem I\'m try开发者_如

Thanks in advance to anyone who takes time to respond. I truly appreciate this site! I've got a function that loads all images it finds in a given directory (see below). The problem I'm try开发者_如何学Pythoning to resolve is that the images are loaded in a random order.

I'd like the images to be loaded by alpha order based on their filename (widget-1.jpg would load before widget-2.jpg for example).

Function is below...

function get_images()
{
global $options;
    foreach ($options as $value) {
    if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}
if($my_custom_images !== "")
{
echo $my_custom_images;
}
else
{
$dir = 'wp-content/uploads/';
$url = get_bloginfo('url').'/wp-content/uploads/';
$imgs = array();
    if ($dh = opendir($dir)) 
    {
    while (($file = readdir($dh)) !== false) 
        {
        if (!is_dir($file) && preg_match("/\.(bmp|jpeg|gif|png|jpg|)$/i", $file)) 
            {
                array_push($imgs, $file);
            }
        }
        closedir($dh);
    } else {
        die('cannot open ' . $dir);
    }

    foreach ($imgs as $idx=>$img) 
    {
        $class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
        echo '<img src="' . $url . $img . '" alt="' .$img . '"' . $class . ' />';
    }
    }
    }


Use sort() on $imgs after you have filled it from the directory


They're probably appearing in directory order, sort-of the order in which they were created. Edit: One solution is popen ("ls") and filter the filenames. I see, php doesn't have such a call.

Anyway, I agree with the first answer: sorting the list after it is built is the most general and portable solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号