How to make Bootstrap Carousel display WordPress Dynamic Content

When code from Twitter’s Bootstrap is not working, one of the main culprits is in linking the files up correctly. It seems to be especially common when adding Bootstrap javascript plugins to a WordPress theme. The Bootstrap files and your jquery library must be called in the proper sequence, along with loads of other scripts being called by other plugins you may be running. I’ve found the popular “WordPress Twitter Bootstrap CSS” plugin to be the best option so far, as it is quite painless and transparent. So Let’s begin.

1) Install ‘WordPress Twitter Bootstrap CSS’ Plugin

Of course you can do it manually, but this method takes 5 seconds, and you can be sure it loads the stylesheets and scripts in their proper places. Btw, it’s best to keep the default option of loading scripts in the footer. If you haven’t already, be sure to add the template tag <?php wp_footer();?> just before the </body> tag in the footer of your theme. The Bootstrap plugin will use this to load it’s necessary javascript.

2) Include the latest jquery library

I’ve added mine just above the wp_footer tag, like so…

[code]






[/code]

Now you can see that the jquery library is called first, and then the bootstrap stuff, as it should be.

3) Grab the Bootstrap Carousel source code

The demo lives here. And here is their source code…

[code]


[/code]

4) Verify your work so far

Before we start modifying this code, we’ll want to verify that the jquery library, the Bootstrap plugin files, and wordpress are all playing together nicely. Cut & Paste the above code into your own PHP template file, and do what you gotta do to view your carousel in a browser. I’ve saved mine as sidebar-carousel.php in my theme directory, so I can call it with <?php get_sidebar(‘carousel’);?> from inside my index.php template.
Just to save some time, I admit I already changed one thing in this code from the original- I added a height dimension to the images. Obviously the specific JPGs won’t show up, but the added height should allow you to see that the carousel is scrolling when right/left buttons are clicked.
Hint: If clicking a right/left button does nothing but add “#myCarousel” to your url, jquery is not linked up properly.

5) Prepare your Content

We’re keeping it as simple as posible here by creating a category named Home Slider and publishing 3 sample posts under that category. If you’ve already got plenty of content to play with, you can skip this step and insert your category of choice as category_name in the next step… you’ll see.

6) Add your first loop using WP_Query

Now that you’ve seen it work out of the box, it’s time to add the tricky part.
You’ll notice the source code provided 3 carousel items, i.e. <div class=”item”>. Our purposes only require 2: the 1st item with the added class “active”; and the 2nd item, which will self-replicate in the query loop we’re about to create. But first we’ll focus on the active carousel item.
We need to surround the <div class=”item active”> with its own WP_Query loop. So just above it, we ask for the latest post from the category Home Slider. Then we end that loop after the active item div with endwhile and the very important wp_reset_postdata fucntion.

[code]

Doug

Doug is an experienced project leader who strives to keep our clients' projects flowing smoothly. As a Developer, Doug is able to ensure a high level of customer satisfaction by focusing his efforts on the setup & support of content management systems (i.e. WordPress & Drupal), custom theme development, CSS/HTML coding, and graphic design. When Doug is not working on web projects, he can be found performing with several professional bands around San Diego.

75 thoughts to “How to make Bootstrap Carousel display WordPress Dynamic Content”

  1. bootstrap carousel and WordPress

    ‘slider’,
    ‘posts_per_page’ => 1,
    )); ?>

    <div class=" item”>

    <a href="” title=””>

    <?php for($i=0; $i
    <li data-target="#carousel" data-slide-to="” class=””>


Leave a Reply

Your email address will not be published. Required fields are marked *