As @MatthewBoynes already stated, making a $.post()
request for every image might bring some overhead.
A maybe better solution would be to grab and cache them, then throw them into your js files using wp_localize_script()
.
$attachments = get_posts( array(
'post_parent' => get_the_ID()
,'post_type' => 'attachment'
,'posts_per_page' => -1
#,'exclude' => get_post_thumbnail_ID() // only needed if we want to exclude the feat. image
) );
// Hook those to `wp_enqueue_scripts`
wp_register_script(
'gallery'
,get_template_directory_uri().'/js/gallery.js'
,array( 'jquery' )
,filemtime( get_template_directory().'/js/gallery.js' )
,true
);
wp_localize_script(
'gallery'
,'gallery_object'
,json_encode( $attachments )
);
Then you can access the gallery_object
from inside your ~/js/gallery.js
file and move to the next image with an .on()
click handler.