我已经将Bootstrap传送带集成到了我的Wordpress中。幻灯片将从标记为“精选”的帖子中获取,因此将仅显示5个最近输入的“精选”帖子。

下面是我的代码:

<div id="carousel-captions" class="carousel slide bs-docs-carousel hidden-xs">
        <ol class="carousel-indicators">
          <li data-target="#carousel-captions" data-slide-to="0" class="active"></li>
          <li data-target="#carousel-captions" data-slide-to="1" class=""></li>
          <li data-target="#carousel-captions" data-slide-to="2" class=""></li>
          <li data-target="#carousel-captions" data-slide-to="3" class=""></li>
          <li data-target="#carousel-captions" data-slide-to="4" class=""></li>
        </ol>
        <div class="carousel-inner">

<?php $the_query = new WP_Query( 'tag=featured&orderby=date&posts_per_page=5' ); ?>

<?php if ( $the_query->have_posts() ) : ?>

  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="item">
          <a href="<?php the_permalink() ?>">
            <img src="<?php the_field('header_banner', $post_id); ?>" alt="">
            <div class="carousel-caption">
              <h3><?php the_field('year', $post_id); ?></h3><span class="name">Make<br><?php $category = get_the_category(); echo $category[0]->cat_name; ?></span><hr>
              <p><?php the_field('mileage', $post_id); ?> Miles | <?php the_field('exterior_color', $post_id); ?> Color<br><br><?php echo homepage_carousel_excerpt(15); ?></p><span class="btn btn-default">Details&nbsp;&nbsp;&nbsp;&rarr;</span>
            </div></a>
          </div>
  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>

<?php else:  ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>


        </div>
        <a class="left carousel-control" href="#carousel-captions" data-slide="prev">
          <span class="icon-prev"></span>
        </a>
        <a class="right carousel-control" href="#carousel-captions" data-slide="next">
          <span class="icon-next"></span>
        </a>
</div>

问题是它不会滑动,因为“事件”类不能静态工作。

我该如何解决?

谢谢

最佳答案

为了避免两次查询,可以在循环外将变量设置为1。在循环中,当“active”类等于1时,将其添加,然后对其进行递增。

<?php
// Previous code here...
$i = 1;
while ( $the_query->have_posts() ) :
    $the_query->the_post();
         ?>
        <div class="item<?php if ($i == 1) echo 'active'; ?>">

        </div>
<?php
    $i++;
endwhile;
wp_reset_postdata();
?>

09-11 19:44