简体   繁体   English

Wordpress循环:如何将每3个帖子包装成一个div?

[英]Wordpress Loop: how to wrap each 3 posts into a div?

I'm trying this: 我正在尝试这个:

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

But it's not working! 但它不起作用! :/ Thank you! :/ 谢谢!

Thanks for your support guys! 谢谢你的支持! :) I tried both solutions but didn't work, I ended up with this and works perfectly! :)我尝试了两种解决方案,但没有工作,我最终得到了这个并且完美无缺!

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

I haven't tested it with wordpress so i'm not 100% sure it'll work. 我还没有用wordpress进行测试,所以我不能100%肯定它会起作用。 The idea is to use the modulus operator (see an example that matches your needs here http://codepad.org/78d2aAKp ) 我们的想法是使用模数运算符(请参阅符合您需求的示例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; ?>
<?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; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM