-1

This may seem a really trivial question.

I have a wordpress loop, and I need to number each looped element ascending.

So it will be like this... 1,2,3,4,5,6,7,8,9,10

Is there some php can do this?

<?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(); ?>

Please see the div where I put number() - a made up function/

Thanks

4

2 回答 2

6
<?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。希望这可以帮助。

于 2013-07-29T15:35:19.583 回答
3
<?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(); ?>

像这样的东西?

于 2013-07-29T15:34:47.137 回答