关于 PHP 中滑块最近帖子的问题

Issue about slider recent posts in PHP

我很尴尬,因为我正在使用滑块来显示最近的帖子。我正在为此使用 Carousel Bootstrap,这是我的代码:

<?php
        $lastposts = get_posts( array(
            'posts_per_page' => 3,
                'category' => 584
        ) );

        if ( $lastposts ) { ?>
            <div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
              <div class="carousel-inner">



            <?php foreach ( $lastposts as $post ) :
                setup_postdata( $post ); ?>
                        <div class="carousel-item active">
                        <img src="/wp-content/uploads/slider.png">
                                <div class="carousel-caption d-none d-md-block">
                                    <div class="title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </div>
                                </div>
                            </div>

            <?php
            endforeach;
            wp_reset_postdata();
        }
?>
</div>
</div>

问题是我所有的项目都是 "active"。

我该如何解决?仅将第一项设为活动项。

用计数器变量试试:

<?php
        $lastposts = get_posts( array(
            'posts_per_page' => 3,
                'category' => 584
        ) );

        if ( $lastposts ) { ?>
            <div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
              <div class="carousel-inner">


            <?php $counter = 1; ?>
            <?php foreach ( $lastposts as $post ) :
                setup_postdata( $post ); ?>
                        <div class="carousel-item <?php echo ($counter==1) ? "active" : ""; ?>">
                        <img src="/wp-content/uploads/slider.png">
                                <div class="carousel-caption d-none d-md-block">
                                    <div class="title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </div>
                                </div>
                            </div>

            <?php
                $counter++;
            endforeach;
            wp_reset_postdata();
        }
?>
</div>
</div>