Php wordpress while循环中的第一篇帖子
我正在尝试使用高级自定义字段在WordPress中创建一个引导转盘 循环中的第一个“转盘项目”必须具有“活动”类。我不知道如何定义if条件,以便将类添加到循环的第一次迭代中 转盘指示器的情况也是如此。 类active应该添加到第一次迭代中,数据slide to=“x”应该是循环的计数器。你知道如何让计数和班级运转吗Php wordpress while循环中的第一篇帖子,php,wordpress,twitter-bootstrap,bootstrap-4,advanced-custom-fields,Php,Wordpress,Twitter Bootstrap,Bootstrap 4,Advanced Custom Fields,我正在尝试使用高级自定义字段在WordPress中创建一个引导转盘 循环中的第一个“转盘项目”必须具有“活动”类。我不知道如何定义if条件,以便将类添加到循环的第一次迭代中 转盘指示器的情况也是如此。 类active应该添加到第一次迭代中,数据slide to=“x”应该是循环的计数器。你知道如何让计数和班级运转吗 <section> <div id="theCarousel" class="carousel slide" data-ride="carousel" da
<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>
下面是我如何实现它的。
定义了两个变量。一个用于传送带指示器,一个用于传送带包装器,并在循环中增加它们
$carousel_wrap=0;
$carousel_ind=0
<section>
<div id="theCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
<ol class="carousel-indicators">
<?php $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
<?php while( $loop->have_posts() ): $loop->the_post(); $carousel_ind++; ?>
<li data-target="#theCarousel" data-slide-to="<?php echo $carousel_ind; ?>" class="<?php if( $carousel_ind == 1 ) echo 'active' ?>""></li>
<?php endwhile; wp_reset_query();?>
</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(); $carousel_wrap++; ?>
<div class="carousel-item <?php if( $carousel_wrap == 1 ) echo 'active' ?>" 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>
“style=”背景图像:url(“”)”>
使用for循环即可
for ( $iteration = 0; $the_query->have_posts(); $iteration++ ) : $the_query->the_post();
endfor;