2011-11-01 9 views
5

sto cercando questo:Wordpress Loop: come avvolgere ogni 3 post in 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; ?> 

Ma non funziona! :/ Grazie!

+0

penso che questo sarebbe il modo più semplice per farlo: stackoverflow.com/questions/28247770/loop-through-wordpress-posts-and-wrap-each-x-post-in-a-div –

risposta

1

Grazie per i vostri ragazzi di supporto! :) Ho provato entrambe le soluzioni ma non ha funzionato, Ho finito con questo e funziona perfettamente!

<?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

Non l'ho provato con wordpress quindi non sono sicuro al 100% che funzioni. L'idea è quella di utilizzare l'operatore modulus (vedere un esempio che soddisfa le vostre esigenze qui 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; ?> 
Problemi correlati