While loop flag variable

user3077627

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

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related