You are using an associative array of $attach_id => $attach_obj
and loop through it. While there are methods to get the next and previous elements in this array, I would prefer to rely on continuous index array to get these objects. First convert the array:
$images = array_values($images);
Now loop through using the contineous index:
foreach ($images as $index => $attachment) {
Finally check for next and previous objects:
<?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 } ?>
Edit: here how your code might look like:
<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 ?>
}
}