Getting WordPress to work with Bootstrap carousel
-
I am so close to getting this to work, but there’s one little problem.
My carousel loop is getting the featured image attached to four posts with the category of “frontpage.”
The Bootstrap carousel needs the first div to have the class of “active,” so I am looping through one post to get that; then I’m using “do not duplicate” to skip the first post when I loop through the others.
I have this code:
<div class="container col-xs-6 col-sm-4"> <div class="carousel slide" id="featured"> <ol class="carousel-indicators" style="bottom:-50px;"> <li style="background-color:#ccc;" data-target="#featured" data-slide-to="0" class="active"></li> <li style="background-color:#ccc;" data-target="#featured" data-slide-to="1"></li> <li style="background-color:#ccc;" data-target="#featured" data-slide-to="2"></li> <li style="background-color:#ccc;" data-target="#featured" data-slide-to="3"></li> </ol> <div class="carousel-inner"> <?php $args = array( 'category_name' => 'frontpage', 'posts_per_page' => 1 ); ?> <?php $my_query = new WP_Query( $args ); while ( $my_query->have_posts() ) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?> <div class="item active"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> </div> <?php endwhile; ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); if ( $post->ID == $do_not_duplicate ) continue; ?> <div class="item"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> </div> <?php endwhile; endif; ?> </div><!-- carousel-inner --> <a class="left carousel-control" href="#featured" role="button" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"> </span> </a> <a class="right carousel-control" href="#featured" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"> </span> </a> </div><!-- carousel --> </div><!-- container -->
The problem is the second loop is picking up posts from all categories, not just those with the frontpage category.
If I change the first line of the second loop to:
<?php if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); if ( $post->ID == $do_not_duplicate ) continue; ?>
… I break it. I don’t get the last three images at all.
If this is the wrong approach, I’m happy to try something else. Thank you.
- The topic ‘Getting WordPress to work with Bootstrap carousel’ is closed to new replies.