当前位置: 动力学知识库 > 问答 > 编程问答 >

php - While loop flag variable

问题描述:

I am trying to implement a bootstrap carosel into wordpress, but I am having problems in using a flag variable in a while loop.

The first slide should come out as: <div class="item active"> and the rest as <div class="item"> However, they are all coming out as active and causing all the images to show.

How can I best implement this?

Thanks

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

网友答案:

Just set flag to false after the first loop:

<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>
网友答案:

You can increment the variable for example like don't increment in the first loop increment as from second

<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>
分享给朋友:
您可能感兴趣的文章:
随机阅读: