我在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 回答
创建一个计数器变量,在循环内增加,然后使用模数来确定它是否应该具有活动类 . 在伪代码中,例如:
一个例子
将输出
print
添加了一个增量变量和一个if语句 .这是您的完整代码