2

I have the following code:

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

And I need to add the class "active" to the first div (next to "item")

4 Answers 4

4

Use a boolean variable to test, set it to true after first pass so that further loops will not mark it active

<?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; ?>
Sign up to request clarification or add additional context in comments.

Comments

2

Add a flag:

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

Comments

0
if($loop->current_post == 1){
  echo 'class';
}

Comments

0
<script type="text/javascript">
$('.carousel-inner > :first-child').addClass("active");
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.