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>
I’ve found that for things like this,
get_posts
is easier.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
Read “Multiple Loops in Action at codex page” i think you´ll have your answer there…at least i had mine : http://codex.wordpress.org/The_Loop
I did use one “featured” category. And the query was made by “Multiple loops in Action”. First loop with just one post with the featured category to put the bootstrap carousel class active. Then the other loop put the other categories less the first.
Hope it helped.
It’s not entirely clear what you are asking, you may want to put some actual questions in your post.
I think one of the things you want is to know how to have
WP_Query
select a specific number of posts, by default WP_Query is designed for paginating through all posts but you don’t have to use the pagination. But to tell WP_Query to only select N number of posts for the “first page” you can use theshowposts
parameter.For meta values you probably want get_post_meta(), but if you are looking to use meta values in your WP_Query you’ll need meta_query.
CSS offers a
:first-child
pseudo-selector to cover the first element in a container. Otherwise, while this probably isn’t the place for it, you can just set up a count variable outside the loop and do something likeif ($count++ == 0) $class = 'active';
inside.You need to write a conditional statement to have the active class loop through the posts this is the technique that I used along with custom post types.
`
`