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 に答える