Display custom post type and custom fields within a Bootstrap Carousel

I’ve found that for things like this, get_posts is easier.

<?php
    // Set up your arguments array to include your post type,
    // order, posts per page, etc.

    $args = array(
        'post_type' => 'testimonial',
        'posts_per_page' => 3,
        'orderby' => 'date',
        'order' => 'DESC'
    );

    // Get the posts
    $testimonials = get_posts($args);

    // Loop through all of the results
    foreach ($testimonials as $testimonial)
    {
        // Since we're doing this outside the loop,
        // Build the apply the filters to the post's content

        $content = $testimonial->post_content;
        $content = str_replace(']]>', ']]&gt;', $content);
        $content = apply_filters('the_content', $content);

        // Build out the carousel item
?>
        <div class="item-active">
            <?php echo get_post_thumbnail($testimonial->id); ?>
            <div class="carousel-caption">
                <h4><?php echo $testimonial->post_title; ?></h4>
                <?php echo $content; ?>
            </div>
        </div>
<?php
    }
?>

This has worked for me so many times that it’s become my go-to method for all of my carousels, jQuery or Twitter Bootstrap.

I really hope this helps.

Codex Function Reference for get_posts

Leave a Comment