2011-11-01 21 views
5

estoy tratando esto:Wordpress Loop: ¿cómo envolver cada 3 publicaciones en un div?

<?php query_posts('cat=6'); ?> 
<?php if (have_posts()) : ?> 

    <?php while (have_posts()) : the_post(); ?> 
     <div> 
      <?php $counter=3; ?> 
      <?php the_post_thumbnail(); ?> 
      <?php $counter++; ?> 
     </div> 

    <?php endwhile; ?> 
<?php endif; ?> 

Pero no está funcionando! :/ ¡Gracias!

+0

creo que esta sería la forma más fácil de hacer esto: stackoverflow.com/questions/28247770/loop-through-wordpress-posts-and-wrap-each-x-post-in-a-div –

Respuesta

1

Gracias por su apoyo chicos! :) Intenté ambas soluciones pero no funcionó, ¡Terminé con esto y funciono perfectamente!

<?php query_posts('cat=6'); ?> 

<?php $variable=0;?> 

<div> 
<?php while (have_posts()) : the_post(); ?> 
<?php if(($variable+1)<4){ ?> 
<a href="<?php echo get_post_meta($post->ID, 'colaborador-link', true); ?>" target="blank"> 
<?php the_post_thumbnail(); ?> 
</a> 
<?php $variable+=1; ?> 
<?php }else{ ?> 
<?php $variable=1; ?> 
</div> 

<div> 
<a href="<?php echo get_post_meta($post->ID, 'colaborador-link', true); ?>" target="blank"> 
<?php the_post_thumbnail(); ?> 
</a> 
<?php }?> 
<?php endwhile; ?> 
</div> 
0

No lo he probado con wordpress, así que no estoy 100% seguro de que funcione. La idea es utilizar el operador modulus (ver un ejemplo que satisfaga sus necesidades aquí http://codepad.org/78d2aAKp)

<?php query_posts('cat=6'); ?> 
<?php if (have_posts()) : ?> 
<!-- Your div starts here --> 
<div> 
<?php 
    while (have_posts()) : 
     the_post(); 
     $counter = 0; 
     if($counter%3 == 0 && $counter > 0): 
?> 
<!--Close and then open the div--> 
</div><div> 
<?php 
     endif; 
?> 
<?php the_post_thumbnail(); ?> 
<?php $counter++; ?> 
<?php endwhile; ?> 
</div><!--/Your div ends here --> 
<?php endif; ?> 
0
<?php query_posts('cat=6'); ?> 
<?php if (have_posts()) : ?> 
<?php $counter=0; ?> 
<?php while (have_posts()) : the_post(); ?> 
<?php if($counter%3==0) : ?>   
<div> 
     <?php $counter=3; ?> 
     <?php the_post_thumbnail(); ?> 
     <?php $counter++; ?> 
</div> 
<?php else: 
//Some code for other posts.. 
endif; 
?> 
<?php $counter++; ?> 
<?php endwhile; ?> 
<?php endif; ?> 
Cuestiones relacionadas