如何使用php为每个循环元素添加一个数字


How to add a number to each looped element with php?

这似乎是一个非常琐碎的问题。

我有一个wordpress循环,我需要对每个循环元素进行升序编号。

所以会是这样。。。1,2,3,4,5,6,7,8,9,10

有没有php可以做到这一点?

<?php query_posts( 'posts_per_page=10' ); if ( have_posts() ) : ?>
     <?php while ( have_posts() ) : the_post(); ?>
          <div id="list-<?php echo number(); ?>">
               <?php get_template_part('item'); ?>
          </div>
     <?php endwhile; ?>
<?php endif; wp_reset_query(); ?>

请参阅我放置number()的div-一个虚构的函数/

感谢

<?php query_posts( 'posts_per_page=10' ); if ( have_posts() ) : ?>
  <?PHP $i=0; ?>
     <?php while ( have_posts() ) : the_post(); ?>
          <div id="list-<?php echo $i++; ?">
               <?php get_template_part('item'); ?>
          </div>
     <?php endwhile; ?>
<?php endif; wp_reset_query(); ?>

它的基本作用是创建一个值为0的变量。每次它通过while循环时,都会使用++添加1。希望这能有所帮助。

<?php query_posts( 'posts_per_page=10' ); if ( have_posts() ) : ?>
<?php $count = 0; ?>
     <?php while ( have_posts() ) : the_post(); $count++; ?>
          <div id="<?php echo number(); ?>" class="item-<?php echo $count ?>">
               <?php get_template_part('item'); ?>
          </div>
     <?php endwhile; ?>
<?php endif; wp_reset_query(); ?>

像这样的东西?