I’d use the this code to print the image instead of using the_post_thumbnail();
.
So that the $slider_query->current_post
can be used to print the current iteration of post in WP_Query loop.
Eg.
<img src="https://wordpress.stackexchange.com/questions/61017/<?php wp_get_attachment_url( get_post_thumbnail_id() ); ?>" alt="the-alt-tag" data-caption="#htmlCaption-<?php $slider_query->current_post; ?>" />
Update –
Untested but it should work. Make sure you turn on WordPress debug before development.
<?php
// Get the latest 5 sticky posts from the 'events' post type
$args = array(
'post_type' => 'event',
'posts_per_page' => 5,
'post__in' => get_option( 'sticky_posts' )
);
$slider_query = new WP_Query( $args );
// Display the featured images in div
echo '<div id="featured">';
while ( $slider_query->have_posts() ) : $slider_query->the_post() ;
echo '<img src="'.wp_get_attachment_url( get_post_thumbnail_id() ).'" alt="the-alt-tag" data-caption="#htmlCaption-'.$slider_query->current_post.'" />';
endwhile;
$slider_query->rewind_posts();
echo '</div>';
// Print the captions
while ( $slider_query->have_posts() ) : $slider_query->the_post();
echo '<span class="orbit-caption" id="htmlCaption-'.$slider_query->current_post.'><a href="';
the_permalink();
echo '">';
the_title();
echo '</a></span>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>