是否可以按日期对图像进行排序,然后限制为最近显示的四个。
<div class="row no-gutter">
<?php
$dirname = "assets/img/portfolio/";
$images = glob($dirname."*.jpg");
$ignore = Array(".", "..");
foreach($images as $curimg){
$name2 =pathinfo($curimg, PATHINFO_FILENAME);
if(!in_array($curimg, $ignore)) {};
?>
<div class="col-sm-3 sm-margin-bottom-30">
<a href="<?php echo "$curimg"?>" rel="gallery4" class="fancybox img-hover-v2" title="<?php echo "$name2"?>">
<span><img class="img-responsive" src="<?php echo "$curimg"?>" alt=""></span>
</a>
</div>
<?php }?>
</div>
谢谢
答案 0 :(得分:0)
首先,您需要按日期对数组进行排序,然后选择最近的4个:
function sortByDate($a, $b)
{
$aTime = filemtime( $a );
$bTime = filemtime( $b );
if ( $aTime === $bTime ) return 0;
return ( $aTime < $bTime ) ? -1 : 1;
}
$images = glob($dirname."*.jpg");
//Sort with our function
usort( $images, "sortByDate" );
$lastFour = array_slice( $images, -4 );