When i am on the single.php page, i show the first image attachment. then i would like to load the next (and previous) image attachments without reloading the page.
so in single.php i started with this to retrieve an array with the images html:
<?php $args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'order'=> 'ASC', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$my_image = wp_get_attachment_image($attachment->ID, large) );
$attachments_list[] = $my_image;
}
}
reset($attachments_list);
$first_image = current($attachments_list);
echo $first_image;
?>
and then in the script.js file i would like to get the value of my php array $attachments_list to do something like this :
$(document).ready(function(){
var count = attachments_list.lenght();
var init = 0;
var current = init;
if (count > 1) {
$('#next-img').click(function() {
if (current+1 < count) {
current++
} else if (current+1 === count) {
current = init;
}
$('#image-container').html(attachments_list[current]);
});
$('#previous-img').click(function() {
if (current > init) {
current--
} else if (current === init) {
current = count-1;
}
$('#image-container').html(attachments_list[current]);
});
}
});
how can i retrieve the value of the php $attachments_list array to attachments_list inside the script.js file ? Json maybe ?
thanks for your attention
wp_localize_script
.