Carousel with wordpress loop in columns

Asked

Viewed 689 times

1

Hello, I’m trying to adjust my Carousel so that it has a grid done by loop, I have the following wordpress code:

<?php 
global $posts; 
$b = 0;
$args = array( 'post_type' => 'banner', 'posts_per_page' => 5 );
$loop = new WP_Query( $args );
?>
<div id="carousel-front-page" data-ride="carousel" class="carousel slide">
	<div class="carousel-inner">
		<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
			<div class="item <?php if (0 == $b) {echo "active";} ?>">
				<?php the_content() ?>
			</div>
			<?php $b++ ?>
		<?php endwhile ?>
	</div>
	<a class="left carousel-control" href="#carousel-front-page" data-slide="prev">
		<span class="icon-prev"></span>
	</a>
	<a class="right carousel-control" href="#carousel-front-page" data-slide="next">
		<span class="icon-next"></span>
	</a>
</div>

Works normally with wordpress post type loop, but I’m now trying to do the same, only with a grid of col-Md-3 for example, with loop leaving 4 posts starting, and then go for four more. And I’m not getting a loop out of it at all, I don’t know where I’m going wrong, I know that no loop is easy to make.

I did something on top of it, but I’m sinning on something:

<?php 
$per_page = 5;
$n=0;
$args = array( 'post_type' => 'meusprodutos', 'posts_per_page' => $per_page );
$loop = new WP_Query( $args );
if($loop->have_posts()):  
  ?>
<div class="carousel-loja">
 
   <div id="shopCarousel" class="carousel slide">
    <div class="ponteiros">
      <ol class="carousel-indicators">
        <?php  while($loop->have_posts()): $loop->the_post(); ?>
          <li data-target="#shopCarousel" data-slide-to="<?php echo $n++; ?>"></li>
        <?php endwhile; ?>
      </ol>
    </div><!--ponteiros-->

    <!-- Carousel items --> 
    <div class="carousel-inner">
    
     <div class="row">

      <?php  while($loop->have_posts()): $loop->the_post(); ?>
      
      	<div class="col-md-4">   
      <div class="item <?php if (0 == $n) {echo "active";} ?>">
       
            <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>

      </div><?php $n++ ?>
      </div>
    <?php endwhile; ?>
    
    
    </div>
    
  </div>

</div>
</div>
<?php 
endif; 
wp_reset_query(); 
?>

Someone can help me?

  • How so, a grid of col-Md-3 for example, with loop leaving 4 start posts, and then go for four more. Like, you want me to press 4 posts once to stay in Carousel, and then when you do one refresh of the page, are 4 posts different?

  • In fact, I want to appear more than 1 post at once in Carousel, like this one: http://bootsnipp.com/snippets/featured/infinite-carousel-loop but I don’t know how to loop this way, only with 1 image at a time

  • Ahh, now I get it. Your problem then lies in the fact that you can’t make more than one photo/post appear at a time?

  • Exactly, that’s what I need to do

  • And I suppose you already have the right CSS and JS? The problem is simply not showing more than one? If you can, post Javascript (the $('#mycarousel').carousel()) and associated CSS (not bootstrap)

  • I’m using the bootstrap pattern, the code I posted is pure, I made a post type only, works cool 1 at a time, a standard slide.. Only now I want to mount a slide like this, that appears more than one post at a time, da para usar o grid do bootstrap, but when it makes the loop is more complicated and I’m getting stuck

  • As I mentioned in my reply, see if you can capture some images from your site (or even a link if you can, to see "personally").

Show 2 more comments

1 answer

0


Taking the code you showed me you’re using as your example:

http://bootsnipp.com/snippets/featured/infinite-carousel-loop

The main code is this:

<div class="item active">
  <div class="col-lg-4 col-xs-4 col-md-4 col-sm-4">
  <a href="#"><img src="http://lorempixel.com/400/200/sports" class="img-responsive">1</a></div>
</div>

I think the loop you’re using is good, although I don’t know if the_content() even contains an image or not.

Plus, make sure your CSS has it:

.carousel-inner .active.left { left: -33%; }
.carousel-inner .next        { left:  33%; }
.carousel-inner .prev        { left: -33%; }
.carousel-control.left,.carousel-control.right {background-image:none;}
.item:not(.prev) {visibility: visible;}
.item.right:not(.prev) {visibility: hidden;}
.rightest{ visibility: visible;}

And in your Javascript, have this:

$('.carousel .item').each(function(){
  var next = $(this).next();
  if (!next.length) {
    next = $(this).siblings(':first');
  }
  next.children(':first-child').clone().appendTo($(this));

  if (next.next().length>0) {

      next.next().children(':first-child').clone().appendTo($(this)).addClass('rightest');

  }
  else {
      $(this).siblings(':first').children(':first-child').clone().appendTo($(this));

  }
});

Note: I got these codes straight from the sample site you sent

If you already have all this and it’s still not working, try some photos of the rendered results, and include the Javascript and CSS that you have, and any errors that are in the site console or in PHP.

  • I edited my question with the current code I’m trying to make work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.