How can I write the number or id of the post in the data-elem? I have each data-elem. There must be a unique number so that, for example, the first element has data-elem = "1" and in the second data-elem = "2" and so on ... tried this option but does not work, data-elem="<?php $count = $custom_posts->post_count; ?>"

 <?php $query = new WP_Query( 'cat=86' ); ?> <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <div class="faq__item" data-elem=""> <div class="faq__header"><?php the_title(); ?></div> <div class="faq__body"><?php the_content(); ?></div> </div> <?php endwhile; wp_reset_postdata(); else : ?> <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?> 

    1 answer 1

    Everything is solved with the simplest increment

     <?php $query = new WP_Query( 'cat=86' ); ?> <?php $post_num = 0; ?> <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php $post_num++; ?> <div class="faq__item" data-elem="<?= $post_num; ?>"> <div class="faq__header"><?php the_title(); ?></div> <div class="faq__body"><?php the_content(); ?></div> </div> <?php endwhile; wp_reset_postdata(); else : ?> <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?> 

    To output the same $post->ID ID, use $post->ID or the_ID()