wordpress 的 while 循环中的第一个 post
first post in while loop of wordpress
我正在尝试使用高级自定义字段在 WordPress 中创建 bootstrap 轮播。
循环中的第一个 'carousel-item' 必须有 class 'active'。我无法弄清楚如何定义 if 条件,以便将 class 添加到循环的第一次迭代中。
轮播指示器也是如此。
class active 应该添加到第一次迭代中,data-slide-to="x" 应该是循环的计数器。知道如何获得计数和 class 工作吗?
<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>
为此使用 for 循环。
for ( $iteration = 0; $the_query->have_posts(); $iteration++ ) : $the_query->the_post();
endfor;
我正在尝试使用高级自定义字段在 WordPress 中创建 bootstrap 轮播。
循环中的第一个 'carousel-item' 必须有 class 'active'。我无法弄清楚如何定义 if 条件,以便将 class 添加到循环的第一次迭代中。
轮播指示器也是如此。 class active 应该添加到第一次迭代中,data-slide-to="x" 应该是循环的计数器。知道如何获得计数和 class 工作吗?
<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>
为此使用 for 循环。
for ( $iteration = 0; $the_query->have_posts(); $iteration++ ) : $the_query->the_post();
endfor;