在Wordpress中,我希望每三个帖子都有一个div(因为帖子在网格中,每行三个,我希望每一行都有一个统一的高度,所以"阅读更多&#34 ;按钮排在底部 - http://restartcomputer.com/category/products/mac-products/)。我(逻辑上)想出了如何做到这一点 - 它基本上在这个问题的接受答案中概述:PHP loop: Add a div around every three items syntax
但是,我已经尝试了所有方法,无法让代码工作。根本没有添加div。这是代码:
if (have_posts()) :
$counter = 1; ?>
<div class="entries-wrapper">
<?php while (have_posts()) : the_post(); ?>
//post stuff
<?php if ($counter % 3 == 0) { ?>
</div><div class="entries-wrapper">
<?php }
$counter += 1; ?>
<?php endwhile; ?>
</div>
//some more code
<?php endif; wp_reset_query(); ?>
知道为什么吗?
答案 0 :(得分:1)
您还没有回到脚本(第4行):
if (have_posts()) :
$counter = 1; ?>
<div class="entries-wrapper">
<?php while (have_posts()) : the_post(); ?>
//post stuff
<?php if ($counter % 3 == 0) { ?>
</div><div class="entries-wrapper">
<?php }
$counter += 1; ?>
<?php endwhile; ?>
</div>
//some more code
<?php endif; wp_reset_query(); ?>
我在主题中做了类似的事情来并排调整帖子:
<?php if ( have_posts() ) : ?>
<?php $col = 1; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if($col == 1) echo "<div class='row'>"; ?>
<div class="post col<?php echo $col; ?>" id="post-<?php echo the_ID(); ?>">
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php (($col==1)?$col=2:$col=1); ?>
</div>
<?php if($col == 1) echo "</div>"; ?>
<?php endwhile; ?>
<?php kubrick_paging_nav(); ?>
<?php else : ?>
也可以尝试在WHILE语句中移动DIV。