我有一个图像数组循环,它成功遍历我的图像项。但是,我需要为每个项目赋予'data-slide-index'属性一个唯一的编号,从0开始,为每个后续项添加1(如果总共有10个图像,则为0-9)。我意识到这可能是简单的东西,但我是一个PHP新手,所以真的很感谢关于最好,最简单的方法。
<?php $images = get_post_meta(get_the_ID(), "images", true);
$images = unserialize($images);
foreach($images as $image) {
$ar[] = array("order" => $image['order'], "img_id" => $image['image_id'], "desc" => $image["desc"]);
}
asort($ar);
foreach($ar as $item) {
$image_id = $item['img_id'];
$media_med = wp_get_attachment_image_src( $image_id, "medium", false);
$media_full = wp_get_attachment_image_src( $image_id, "full", false);
echo "<a data-slide-index='" . $count ."' href=''><img data-title='" . $item["desc"] . "' data-big='". $media_full[0] . "' src='" . $media_med[0] . "'></a>";
} ?>
答案 0 :(得分:1)
<?php $images = get_post_meta(get_the_ID(), "images", true);
$images = unserialize($images);
foreach($images as $image) {
$ar[] = array("order" => $image['order'], "img_id" => $image['image_id'], "desc" => $image["desc"]);
}
asort($ar);
//Create count variable
$i=0;
foreach($ar as $item) {
$image_id = $item['img_id'];
$media_med = wp_get_attachment_image_src( $image_id, "medium", false);
$media_full = wp_get_attachment_image_src( $image_id, "full", false);
//assign the number to the slide index
echo "<a data-slide-index='".$i."' href=''><img data-title='" . $item["desc"] . "' data-big='". $media_full[0] . "' src='" . $media_med[0] . "'></a>";
//plus up the variable for each loop item
$i++;
} ?>