0

I am trying to write a foreach loop for a Bootstrap Carousel in PHP, but the problem I'm having is, the first slide's div must be set as "active" but not the rest, and the li's "data-slide-to" must be set to the correct number, making the first one "[0]" have a class of active, but not the rest...

I know how to get the first result of the foreach, but I don't know how to call the second and beyond separately, nor how to make it match it to the "data-slide-to".

Here is my code below.
Disclaimer: the PHP code is Wordpress-specific, but the answer to the problem does not involve Wordpress-specific code.

PHP

$query_images_args = array(
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'post_status'    => 'inherit',
    'posts_per_page' => - 1,
);

$query_images = new WP_Query( $query_images_args );

$images = array();
foreach ( $query_images->posts as $image ) {
    $images[] = wp_get_attachment_url( $image->ID );
}

for($i = 0; $i < count($images); $i++) {
$imageURL = $images[$i];

// I would echo all the entries with: 
// echo $imageURL;

}

HTML

<div id="myCarousel" class="carousel slide">

        <!-- Indicators -->
        <ol class="carousel-indicators">
//Here's the active "li", which would be set to $imageURL[0]
            <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
//Here are the rest of the "li"s , which idk how to set up?
            <li data-target="#myCarousel" data-slide-to="1"></li>
            <li data-target="#myCarousel" data-slide-to="2"></li>
        </ol>


        <!-- Wrapper for Slides -->

        <div class="carousel-inner">
            <div class="item active">
                <!-- Set the first background image using inline CSS below. -->
//Again, I know to set the background-image:url to  '<?php echo $imageURL[0] ?>'
                <div class="fill" style="background-image:url('img/01.png');"></div>
                <div class="carousel-caption">
                    <h2>Caption 1</h2>
                </div>
            </div>
            <div class="item">
                <!-- Set the second background image using inline CSS below. -->
//Not sure about here...
                <div class="fill" style="background-image:url('img/02.png');"></div>
                <div class="carousel-caption">
                    <h2>Caption 2</h2>
                </div>
            </div>
            <div class="item">
                <!-- Set the third background image using inline CSS below. -->
//This part needs to be covered by the 2nd imageURL and beyond...
                <div class="fill" style="background-image:url('img/03.png');"></div>
                <div class="carousel-caption">
                    <h2>Caption 3</h2>
                </div>
            </div>
        </div>


        <!-- Controls -->

        <a class="left carousel-control" href="#myCarousel" data-slide="prev">
            <span class="icon-prev"></span>
        </a>
        <a class="right carousel-control" href="#myCarousel" data-slide="next">
            <span class="icon-next"></span>
        </a>

    </div>

Not sure if I'm approaching it correctly, but I cannot get it to work.

user122552
  • 71
  • 3
  • 9
  • 1
    As Disclaimer: said you don't need wordpress specific code as answer, Possible duplicate of [create a slideshow of images inside bootstrap modal dynamically using PHP](http://stackoverflow.com/questions/33564084/create-a-slideshow-of-images-inside-bootstrap-modal-dynamically-using-php) – Shehary Feb 01 '16 at 06:04

2 Answers2

1
<?php 

**//for indicators**

foreach($yourArray as $key=>$result){

     <li data-target="#myCarousel" data-slide-to="<?php echo $key; ?>" class="<?php if($ke==0){echo "active";} ?>"></li>
}

For Your Image

foreach($yourArray as $key=>$result){

     <div class="item <?php if($key==0){echo "active";} ?>">
                <div class="fill" style="background-image:url('img/01.png');"></div> // Here is Your Image Url 
                <div class="carousel-caption">
                    <h2>Caption 1</h2>
                </div>
            </div>
}

Hope This Will Gives You an Idea.

santosh
  • 799
  • 5
  • 17
0

You've probably solved this a long time ago, but this is what I did to solve the first image being set as 'active'.

echo '<div class="item active">'.$images[0].'</div>';
unset($images[0]);
foreach ($images as $img){
   echo '<div class="item">'.$img.'</div>';
}