2011-11-01 34 views
5

Tôi đang thử điều này:Wordpress Loop: cách quấn 3 bài đăng vào một 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; ?> 

Nhưng nó không hoạt động! :/ Cảm ơn bạn!

+0

Tôi nghĩ đây sẽ là cách dễ nhất để thực hiện việc này: stackoverflow.com/questions/28247770/loop-through-wordpress-posts-and-wrap-each-x-post-in-a-div –

Trả lời

1

Thanks for guys hỗ trợ của bạn! :) Tôi đã thử cả hai giải pháp nhưng không hoạt động, Tôi đã kết thúc với điều này và hoạt động hoàn hảo!

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

Tôi chưa thử nghiệm nó với wordpress vì vậy tôi không chắc chắn 100% nó sẽ hoạt động. Ý tưởng là để sử dụng toán tử modulus (xem một ví dụ phù hợp với nhu cầu của bạn ở đây 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; ?> 
Các vấn đề liên quan