Adding a Bootstrap slideshow to WordPress

After the last tutorial on how to Add Bootstrap to a WordPress theme it’s now time to do something useful with Bootstrap. Let’s make a slideshow that uses the featured images of the posts in a “Slideshow category”.

If you are using Bootstrap the good news is that it already has a carousel component by default which we can use and adapt for WordPress use. Let’s just copy the example code from the Bootstrap site and replace the img src with some pictures already uploaded in wordpress. Open the page template where you want to display the carousel (for example index.php) and paste the code below under the get_template_part( ‘header’ ); line. Make sure you replace the images with your own images:

[gist id=”d037158761b46833d80b7bd8488440e0″]

The result show look something like this:

Let’s now change the code and have the slidehow content created dynamically from posts in a specific category. Create a category, let’s say “Slideshow” from the dashboard, then click on edit category and check the id of the new category in the URL, for example:

Now, add some posts to the category, making sure they have text and a featured image. The portion of the text will be used as caption, and the featured image as the slideshow image.

Good, now we need to change the slideshow code and add the wordpress loop. First, there are 3 slides in the example, and as we need to cycle using the WordPress loop we only need one. So we only need one recurrence of the carousel-inner div.

Let’s prepare the WordPress loop and make it read the “Slideshow” category. To read the posts of the slideshow category you need to send the category id to the loop by setting loop arguments.

<div class="carousel-inner">
 <div class="carousel-item active"><img class="d-block w-100" src="" alt="..." />
 <div class="carousel-caption d-none d-md-block">
 <h5>First slide label</h5>
 Some representative placeholder content for the first slide.


If you are running this code you will see that it’s not working. Here is the hint from the bootstrap site: The .active class needs to be added to one of the slides otherwise the carousel will not be visible. This means we need to add the active class to only one slide. But the loop repeats the same code 3 times. We need a counter – it’s easier than you might think:

$count=0; // here we add a variable
$args = array(
$wp_query = new WP_Query( $args );

if ( $wp_query->have_posts()) : while ( $wp_query->have_posts()) : $wp_query->the_post();
$count ++; // here we implement the increment

<div class="carousel-item <?php if($count=="1") {echo 'active'; // here we set the active class if it's the first occurence} ?>">
<img src="" class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>First slide label</h5>
<p>Some representative placeholder content for the first slide.</p>

<?php endwhile;  endif; ?>

We are all good now, just need to replace the hardcoded content with content from the loop. For this we will use the usual the_content(),the_title(). For the thumbnail, if you look at the html title we should also add a class: d-block w-100. For this we will use the_post_thumbnail with a class array like this:
Here is the final code:

[gist id=”1f32581e7b4d97f5a5e6180016a5ae06″]