I am using the “Types” wordpress plugin for the first time. I created a custom post type, as well as some custom fields.

How do I dynamically call all the custom posts into a Bootstrap carousel, and also display the fields within that loop?

And how do I limit the amount of posts that will cycle through the carousel? Another requirement is that I need to add that necessary Bootstrap ‘active’ class to only the first post.

Here is my first shot, (also note that I have some custom js for creating pagination for the carousel, but that is not an issue (so far!))

<!-- need to limit the entire carousel to displaying the 5 latest posts -->
    <div id="myCarousel2" class="carousel">
        <div class="carousel-inner2">

            <?php $loop = new WP_Query( array( 'post_type' => 'testimonial' ) ); ?>
            <div class="item active" data-title=""><!-- the first div needs a class of active, but the others do not -->
                <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                    <div class="slide-copy"><?php the_content(); ?></div>
                    <!-- need to display a custom field or two here -->
                <?php endwhile; ?>
          </div>
        </div>
      </div><!-- end myCarousel -->

Here is my second attempt. I got this to work beautifully, except for displaying the custom field values. Any suggestions? It looks like the syntax is correct… Am I missing something basic here?

<?php $the_query = new WP_Query(array(
'post_type' => 'testimonial', 
'posts_per_page' => 1 
));
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="item active" data-title="">
    <div class="slide-copy">
        <?php the_content();?>
        <span class="byline"><?php echo get_post_meta($post->ID, 'authorinfo', true); ?></span>
    </div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php $the_query = new WP_Query(array(
'post_type' => 'testimonial', 
'posts_per_page' => 5, 
'offset' => 1 
));
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="item" data-title="">
    <div class="slide-copy">
    <?php the_content();?>
    <span class="byline"><?php echo get_post_meta($post->ID, 'authorinfo', true); ?></span>
    </div>
</div>

4 s
4

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 Reply

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