while循环标志变量

时间:2014-03-22 17:03:40

标签: php wordpress

我正在尝试将一个bootstrap carosel实现到wordpress中,但是我在while循环中使用flag变量时遇到了问题。

第一张幻灯片应该显示为:<div class="item active">,其余的显示为<div class="item">但是,它们全部显示为活动状态,并显示所有图片。

我怎样才能最好地实现这个目标?

由于

<div id="col-sm-12">
    <div id="this-carousel-id" class="carousel slide">
        <div class="carousel-inner">
<?php 
        $flag = 0;
            $loop = new WP_Query(array('post_type' => 'feature', 'posts_per_page' => -1, 'orderby'=> 'ASC')); 
            ?>
            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <div class="item <?=$flag==0?"active":""?>">
                    <?php $url = get_post_meta($post->ID, "url", true);
                    if($url!='') { 
                        echo '<a href="'.$url.'">';
                        echo the_post_thumbnail('full');
                        echo '</a>';
                    } else {
                        echo the_post_thumbnail('full');
                    } ?>

                </div>
            <?php endwhile; ?>

            <?php wp_reset_query(); ?>
        </div>  
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

在第一个循环后将flag设置为false:

<div id="col-sm-12">
    <div id="this-carousel-id" class="carousel slide">
        <div class="carousel-inner">
<?php 
        $flag = 1;
            $loop = new WP_Query(array('post_type' => 'feature', 'posts_per_page' => -1, 'orderby'=> 'ASC')); 
            ?>
            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <div class="item <?=$flag?"active":""?>">
                    <?php $url = get_post_meta($post->ID, "url", true);
                    if($url!='') { 
                        echo '<a href="'.$url.'">';
                        echo the_post_thumbnail('full');
                        echo '</a>';
                    } else {
                        echo the_post_thumbnail('full');
                    } ?>

                </div>
            <?php $flag = 0; endwhile; ?>

            <?php wp_reset_query(); ?>
        </div>  
    </div>
</div>

答案 1 :(得分:0)

您可以增加变量,例如从第二个

开始在第一个循环增量中不增加
<div id="col-sm-12">
    <div id="this-carousel-id" class="carousel slide">
        <div class="carousel-inner">
<?php 
        $flag = 0;
            $loop = new WP_Query(array('post_type' => 'feature', 'posts_per_page' => -1, 'orderby'=> 'ASC')); 
            ?>
            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <div class="item <?=$flag==0?"active":""?>">
                    <?php $url = get_post_meta($post->ID, "url", true);
                    if($url!='') { 
                        echo '<a href="'.$url.'">';
                        echo the_post_thumbnail('full');
                        echo '</a>';
                    } else {
                        echo the_post_thumbnail('full');
                    } ?>

                </div>
            <?php
              $flag++;
 endwhile; ?>

            <?php wp_reset_query(); ?>
        </div>  
    </div>
</div>