php - WordPress 的 while 循环中的第一篇文章

标签 php wordpress twitter-bootstrap bootstrap-4 advanced-custom-fields

我正在尝试使用高级自定义字段在 WordPress 中创建 Bootstrap 轮播。

循环中的第一个“轮播项目”必须具有“active”类。我无法弄清楚如何定义 if 条件,以便将该类添加到循环的第一次迭代中。

轮播指示器的情况也是如此。 类 active 应添加到第一次迭代中,并且 data-slide-to="x"应该是循环的计数器。知道如何让计数和类工作吗?

<section>
    <div id="theCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
        <ol class="carousel-indicators">
            <li data-target="#theCarousel" data-slide-to="0" class="active"></li>
            <li data-target="#theCarousel" data-slide-to="1"></li>
            <li data-target="#theCarousel" data-slide-to="2"></li>
        </ol>

        <div class="carousel-inner" role="listbox">
            <?php $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
            <?php while( $loop->have_posts() ): $loop->the_post(); ?>
                <div class="carousel-item" style="background-image: url('<?php the_field('carousel_image'); ?>')">
                    <div class="carousel-caption d-none d-md-block">
                        <h3><?php the_title(); ?></h3>
                        <p><?php the_field('carousel_description'); ?></p>
                    </div>
                </div>
            <?php endwhile; wp_reset_query();?>
        </div>

        <a class="carousel-control-prev" href="#theCarousel" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#theCarousel" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
</section>

最佳答案

你可以这样做

      <div class="carousel-inner" role="listbox">
            <?php 
            $iteration = 0;
            $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
            <?php while( $loop->have_posts() ): $loop->the_post(); $iteration++; ?>
                <div class="carousel-item<?php if( $iteration == 1 ) echo ' active' ?>" style="background-image: url('<?php the_field('carousel_image'); ?>')" data-slide-to="<?php echo $iteration ?>">
                    <div class="carousel-caption d-none d-md-block">
                        <h3><?php the_title(); ?></h3>
                        <p><?php the_field('carousel_description'); ?></p>
                    </div>
                </div>
            <?php endwhile; wp_reset_query();?>
        </div>

        <a class="carousel-control-prev" href="#theCarousel" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#theCarousel" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
</section>

关于php - WordPress 的 while 循环中的第一篇文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46875518/

相关文章:

php - 将选定的值保存在数据库中作为 id

javascript - 删除jquery创建的内容

替换 WordPress 数据库内容时 MySQL 替换不起作用

javascript - 相对于 SCREEN 和 COL div 定位一个 div

php - Mysql 服务器不再与服务器名连接

php - 避免在 PHP 中使用访问修饰符的原因

wordpress - 在 wordpress docker 中编辑默认的 .htaccess

php - 自定义 Wordpress 导航栏

css - 用于光标手抓取的 AngularJS Bootstrap CSS 入口

html - 高度自动不工作