0

I have the following loop to create my carousel, however I need to add the active class on the first iteration only:

<?php
$args = array(
'post_type' => 'homepage_banner',
'orderby'=>'menu_order','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

echo '<div class="item active"> <img alt="';
echo the_title();
echo'" data-src="" src="">
  <div class="container">
    <div class="carousel-caption"><h2>';
    echo the_title();
      echo '</h2><p>';
      echo the_content();
      echo'</p>
    </div>
  </div>
</div>';

endwhile;

?>

1 Answer 1

1

You can do it simply like this:

<?php

 $args = array(
     'post_type' => 'homepage_banner',
     'orderby'=>'menu_order','order'=>'ASC');
 $loop = new WP_Query( $args );
 $active = ' active'; //<============== updated line 1
 while ( $loop->have_posts() ) : $loop->the_post();

     echo '<div class="item'. $active .'"> <img alt="';

     echo the_title();
     echo'" data-src="" src="">
     <div class="container">
        <div class="carousel-caption"><h2>';
             echo the_title();
             echo '</h2><p>';
             echo the_content();
             echo'</p>
         </div>
     </div>
     </div>';
     $active = ''; //<============== updated line 2

 endwhile;

?>
Sign up to request clarification or add additional context in comments.

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.