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

        <div class="slide">
          <div class="slide-top"><?php the_title();?></div>
          <div class="slide-bottom"><?php the_content();?></div>
        <?php the_post_thumbnail();?>
        </div>

      <?php endwhile; ?>

Добрый день есть такой код. Подскажите пожалуйста как сделать что бы у первого элемента из цикла был класс active

К примеру выведет 3 элемента с классом slide а первый будет иметь и класс active

1

2 ответа 2

1

current_post - Индекс отображаемого сообщения.

<?php while($slider -> have_posts()) :  $slider->the_post(); ?>
  <div class="slide <?php if ( $slider->current_post == 0 ) : ?>active<?php endif; ?>">
     <div class="slide-top"><?php the_title();?></div>
     <div class="slide-bottom"><?php the_content();?></div>
     <?php the_post_thumbnail();?>
  </div>
<?php endwhile; ?>  
0
<?php $i=0; while($slider -> have_posts()): $slider->the_post(); ?>
    <div class="slide<?php if ($i === 0) echo ' active';?>">
      <div class="slide-top"><?php the_title();?></div>
      <div class="slide-bottom"><?php the_content();?></div>
    <?php the_post_thumbnail();?>
    </div>
  <?php $i++; endwhile; ?>
2
  • 2
    Он же просит первому, а вы присваиваете второму. 4 дек 2017 в 22:31
  • Спасибо, исправил 6 дек 2017 в 3:30

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.