WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Incrementing number within WP_Query? (5 posts)

  1. sgreve
    Member
    Posted 1 year ago #

    Hey everybody,

    I'm setting up a photo gallery for my WordPress site, and for navigation purposes I need to increment a link's data-slide-index value by 1 for every post.

    So far nothing I've tried as worked, and I suspect it's because I'm using multiple loops on the page via WP_Query.

    Here's my loop. Can anybody tell me what I need to do here?

    <?php $photo_thumbs = new WP_Query('post_type=photo-gallery&posts_per_page=100&order=DESC');
    while ($photo_thumbs->have_posts()) : $photo_thumbs->the_post(); ?>
    <div class="thumb">
    <span class="mid"><a data-slide-index="VALUE TO BE INCREMENTED" href=""><?php the_post_thumbnail('photos_thumbnails'); ?></a></span>
    </div>
    <?php
    endwhile;
    wp_reset_postdata();
    wp_reset_query();
    ?>

    Thanks!

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    <?php $photo_thumbs = new WP_Query('post_type=photo-gallery&posts_per_page=100&order=DESC');
    $c = 0;
    while ($photo_thumbs->have_posts()) : $photo_thumbs->the_post(); $c++; ?>
    <div class="thumb">
    <span class="mid"><a data-slide-index="<?php echo $c;?>" href=""><?php the_post_thumbnail('photos_thumbnails'); ?></a></span>
    </div>
    <?php
    endwhile;
    wp_reset_postdata();
    wp_reset_query();
    ?>
  3. yakbrother
    Member
    Posted 1 year ago #

    Edit: Nevermind, already answered...

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Aww... I was going to call "snap!". ;-)

  5. sgreve
    Member
    Posted 1 year ago #

    Thanks! That's exactly what I needed!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.