3

我有一个循环,它为每个循环的帖子显示一个列表项(用于滑块),如下所示:

<?php while (have_posts()) : the_post(); ?>
<li data-target="#myCarousel" data-slide-to="0"<?php if( $wp_query->current_post == 0 && !is_paged() ) { ?> class="active"<?php } else { ?><?php } ?>></li>
<?php endwhile; ?>

但是,对于它生成的每个列表项,我需要将 data-slide-to 的值增加 1。例如,如果循环有 3 个帖子,则最终结果如下所示:

<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>

如何逐步增加 data-slide-to 值?

4

1 回答 1

6

在 while 循环中添加一个计数器:

<?php 
  //We want to start with 0 so $counter will be -1
  $counter = -1;
  while (have_posts()) : the_post(); $counter++ 
?>

<li data-target="#myCarousel" data-slide-to="<?php echo $counter; ?>"<?php if( $wp_query->current_post == 0 && !is_paged() ) { ?> class="active"<?php } else { ?><?php } ?>></li>

<?php endwhile; ?>
于 2013-09-07T16:39:48.873 回答