首页 文章

如何使第一篇文章和第五篇文章有一个“活跃”的类?

提问于
浏览
0

我在WordPress上使用Bootstrap Carousel,问题是我可以调用它的类别,我将只调用4个帖子为滑块所以它只会显示4个帖子,但我有一个问题,第一个帖子应该有效class因此滑块会将它识别为第一个,而其余的滑块则没有该类 . 但是,如果我第五次再次发布它将有活动类 . 每5个帖子应该有活跃的课程 .

如何使第一篇文章和第五篇文章具有活动类,其余部分没有它?请检查项目活动类 .

<!-- Carousel -->

        <div id="carousel col-md-12" class="carousel slide" data-ride="carousel">
          <ol class="carousel-indicators">
            <li data-target="#carousel-example" data-slide-to="0" class="active"></li>
            <li data-target="#carousel-example" data-slide-to="1"></li>
            <li data-target="#carousel-example" data-slide-to="2"></li>
          </ol>

          <div class="carousel-inner">
        <?php
            $args = array(
                'category_name' => 'carousel',
                 'post_type' => 'post',
                 'posts_per_page' => 4,
                 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
                 );
            query_posts($args);
            $x = 0;
            while (have_posts()) : the_post(); ?>       
                <div class="item active">
                  <a href="#"><img src="http://localhost/wordpress/wp-content/uploads/2016/08/unnamed-file.jpeg" /></a>
                  <div class="carousel-caption">
                    <div class="wow fadeInRight">
                    <h3 style="color: #dbdbdb">Meow</h3>
                    <p style="color: #dbdbdb">Just Kitten Around</p>
                    <a href="#" class="btn btn-slider">more information..</a>
                    </div>
                  </div>

            <?php if ($x == 2) { $x = -1; } $x++; ?>
            <?php endwhile; ?>
          </div>

          <a class="left carousel-control" href="#carousel-example" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left"></span>
          </a>
          <a class="right carousel-control" href="#carousel-example" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right"></span>
          </a>
        </div>
    <!-- End of Carousel -->

3 回答

  • 0

    创建一个计数器变量,在循环内增加,然后使用模数来确定它是否应该具有活动类 . 在伪代码中,例如:

    $counter=0;
    while( $conditions ){
        $class=$counter % 5==0 ? 'active' : '';
    
        echo "<element class='{$class}' />";
        $counter++;
    }
    

    一个例子

    $counter=0;
    while( $i < 20 ){
        $class=$i % 5==0 ? 'active' : 'EMPTY';/* so you can see it */
        echo $class.'
    '; $counter++; $i++; }

    将输出

    active
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    active
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    active
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    active
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    
  • 1
    $x = 0;
            $i = 1;
            while (have_posts()) : the_post(); ?>       
                <div class="item <?php if($i == 1 || $i == 5){ echo 'active'; } ?>">
                  <a href="#"><img src="http://localhost/wordpress/wp-content/uploads/2016/08/unnamed-file.jpeg" /></a>
                  <div class="carousel-caption">
                    <div class="wow fadeInRight">
                    <h3 style="color: #dbdbdb">Meow</h3>
                    <p style="color: #dbdbdb">Just Kitten Around</p>
                    <a href="#" class="btn btn-slider">more information..</a>
                    </div>
                  </div>
    
            <?php if ($x == 2) { $x = -1; } $x++; $i++; ?>
            <?php endwhile; ?>
    
    • 在第一次和第五次循环迭代中向 print 添加了一个增量变量和一个if语句 .
  • 0

    这是您的完整代码

    <!-- Carousel -->
    
    <div id="carousel col-md-12" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
            <li data-target="#carousel-example" data-slide-to="0" class="active"></li>
            <li data-target="#carousel-example" data-slide-to="1"></li>
            <li data-target="#carousel-example" data-slide-to="2"></li>
        </ol>
    
        <div class="carousel-inner">
            <?php
            $args = array(
                'category_name' => 'carousel',
                'post_type' => 'post',
                'posts_per_page' => 4,
                'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
            );
            query_posts($args);
            $x = 0;
            $postcount = 0;
            while (have_posts()) : the_post();
            $postcount ++;
    //        if($postcount % 5 =='0' || $postcount == '1'){
    //            echo 'active';
    //        }
            ?>
            <div class="item <?php echo ($postcount % 5 =='0' || $postcount == '1') ? 'active': '' ; ?>">
                <a href="#"><img src="http://localhost/wordpress/wp-content/uploads/2016/08/unnamed-file.jpeg" /></a>
                <div class="carousel-caption">
                    <div class="wow fadeInRight">
                        <h3 style="color: #dbdbdb">Meow</h3>
                        <p style="color: #dbdbdb">Just Kitten Around</p>
                        <a href="#" class="btn btn-slider">more information..</a>
                    </div>
                </div>
    
                <?php if ($x == 2) { $x = -1; } $x++; ?>
                <?php endwhile; ?>
            </div>
    
            <a class="left carousel-control" href="#carousel-example" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left"></span>
            </a>
            <a class="right carousel-control" href="#carousel-example" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right"></span>
            </a>
        </div>
        <!-- End of Carousel -->
    

相关问题