您正在使用的关联数组$attach_id => $attach_obj
然后循环通过它。虽然有一些方法可以获取此数组中的下一个和上一个元素,但我更愿意依赖连续索引数组来获取这些对象。首先转换数组:
$images = array_values($images);
现在使用连续索引循环:
foreach ($images as $index => $attachment) {
最后检查下一个和上一个对象:
<?php if (isset($images[$index+1])) { ?>
, "next": "<?php echo $images[$index+1]->post_title; ?>"
<?php } ?>
<?php if (isset($images[$index-1])) { ?>
, "prev": "<?php echo $images[$index-1]->post_title; ?>"
<?php } ?>
编辑:下面是代码的外观:
<script>
var clist_autoplay = false;
var ctemp_autoplay = clist_autoplay;
jQuery(document).ready(function($) {
var larr = {
<?php
$num = 1;
$images = array_values($images);
foreach ($images as $key => $attachment) {
$image_img = wp_get_attachment_image_src( $attachment->ID, \'large\' );
$alt = get_post_meta($attachment->ID, \'_wp_attachment_image_alt\', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $attachment->post_content;
?>
"<?php echo $image_title ?>": {
"0": "<?php echo $image_img[0] ?>",
"1": 600,
"2": 400,
"3": "<?php echo $image_title ?>",
"4": "#",
"5": "<?php echo $image_title ?>",
"6": "",
"7": "",
"8": "",
"9": "",
"num": <?php echo $num++; ?>,
"slideTitle": "<?php echo $caption ?>",
"slideContent": "<p><?php echo $description; ?></p>",
"slideNum": null
<?php if (isset($images[$key+1])) { ?>
, "next": "<?php echo $images[$key+1]->post_title; ?>"
<?php } ?>
<?php if (isset($images[$key-1])) { ?>
, "prev": "<?php echo $images[$key-1]->post_title; ?>"
<?php } ?>
}
<?php } // end foreach ?>
}
}