環路

2013-09-23 20 views
2

添加不同類第一個div我有以下代碼:環路

<?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

使用一個布爾變量來進行測試,第一道次之後將其設置爲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; ?> 
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; 
?> 
0
if($loop->current_post == 1){ 
    echo 'class'; 
} 
0
<script type="text/javascript"> 
$('.carousel-inner > :first-child').addClass("active"); 
</script>