2

次のコードがあります。

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<div class="item">
  <?php the_post_thumbnail('full');?>
  <div class="container">
    <div class="carousel-caption">
      <h1>
        <?php the_title(); ?>
      </h1>
      <p>
        <?php the_excerpt(); ?>
      </p>
    </div>
  </div>
</div>
<?php endwhile; ?>

そして、クラス「アクティブ」を最初のdiv(「アイテム」の隣)に追加する必要があります

4

4 に答える 4

4

ブール変数を使用してテストし、最初のパスの後に true に設定して、それ以上のループでアクティブとマークされないようにします

<?php $firstMarked = false; ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item <?php echo !$firstMarked ? "active":"";?>">
  <?php the_post_thumbnail('full');?>
  <div class="container">
    <div class="carousel-caption">
      <h1>
        <?php the_title(); ?>
      </h1>
      <p>
        <?php the_excerpt(); ?>
      </p>
    </div>
  </div>
</div>
<?php $firstMarked = true;?>
<?php endwhile; ?>
于 2013-09-23T21:18:35.077 に答える
2

フラグを追加します。

<?php 
    $isFrist = true;
    while ( $loop->have_posts() ) : $loop->the_post();
?>

<div class="item">
  <?php the_post_thumbnail('full');?>
  <div class="container<?php if ($isFirst): ?> active<?php endif ?>">
    <div class="carousel-caption">
      <h1>
        <?php the_title(); ?>
      </h1>
      <p>
        <?php the_excerpt(); ?>
      </p>
    </div>
  </div>
</div>
<?php
    $isFrist = false;
    endwhile;
?>
于 2013-09-23T21:19:58.393 に答える
0
if($loop->current_post == 1){
  echo 'class';
}
于 2013-09-23T21:23:10.337 に答える