在wordpress的while循环中的第一篇文章

时间:2017-10-22 14:58:18

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

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

第一个' carousel-item'在循环中必须有class' active'。我无法弄清楚如何定义if条件,以便将类添加到循环的第一次迭代中。

旋转木马指示器的情况也是如此。 class active应该被添加到第一次迭代并且data-slide-to =" x"应该是循环的计数器。知道如何让计数和班级工作吗?

% i=0; 
m=70;
C=10;
g=9.81;
% V(0)=0;
% Initialize the arrayV
V(1)=0;
% Define the time increment
dt=0.25
% Define the time samples
t=0:dt:50
% for t=0:0.25:50
% Loop over the time samples
for idx=1:length(t)-1
%    V(i+1)=V(i)+(g-(C*V(i))/m)*(t(i+1)-t(i));
   V(idx+1)=V(idx)+(g-(C*V(idx))/m)*dt;
%    i=i+1;
end
% plot(V(i),t(i))
plot(t,V)
grid minor
xlabel('Time')
ylabel('Speed')

3 个答案:

答案 0 :(得分:1)

你可以这样做

      <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>

答案 1 :(得分:0)

这是我实施的方式。 定义了两个变量。一个用于旋转木马指示器,一个用于旋转木马包装,并在循环中递增它们。

$ 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>

答案 2 :(得分:0)

为此使用for循环。

for ( $iteration = 0; $the_query->have_posts(); $iteration++  ) : $the_query->the_post(); 
endfor;