我需要将博客分为三列。为此,我写道:
<?php $i = 0; ?>
<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 3) == 0) : $wp_query->next_post(); else : the_post(); ?>
<?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>
<?php $i = 0; rewind_posts(); ?>
<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 3) == 1) : $wp_query->next_post(); else : the_post(); ?>
<?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>
<?php $i = 0; rewind_posts(); ?>
<div class="onethird last">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 3) == 2) : $wp_query->next_post(); else : the_post(); ?>
<?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>
我确定这是非常糟糕的代码,但这是我能想到的最好的代码。然后,我注意到重复的帖子问题。再次,经过大量谷歌搜索,错误:
<?php $i = 0; $dupe = array(); ?>
<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if( (($i % 3) == 0) && (!in_array($post->ID, $dupe)) ) : $wp_query->next_post(); else : the_post(); ?>
<?php $dupe[] = $post->ID; echo $post->ID ?>
<?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>
<?php $i = 0; rewind_posts(); ?>
<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if( (($i % 3) == 1) && (!in_array($post->ID, $dupe)) ) : $wp_query->next_post(); else : the_post(); ?>
<?php $dupe[] = $post->ID; echo $post->ID ?>
<?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>
<?php $i = 0; rewind_posts(); ?>
<div class="onethird last">
<?php if (have_posts()) : while(have_posts()) : $i++; if( (($i % 3) == 2) && (!in_array($post->ID, $dupe)) ) : $wp_query->next_post(); else : the_post(); ?>
<?php $dupe[] = $post->ID; echo $post->ID ?>
<?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>
现在它只是忽略计数器和in_array
,并显示所有三列中的所有帖子。
如果有人有更好的解决方案来在三列中显示帖子,那也将受到欢迎!
对于您要实现的目标,三个循环似乎有点复杂。
我会得到帖子总数 - wp_count_posts()
- 并将其除以 3(四舍五入)以获得每列的帖子数。
然后,您只需循环一次,每当计数器的剩余部分除以每列的帖子数$i
时0
,您就会添加</div><div class='onethird'>
。
像这样:
<div class="onethird">
<?php
$a_third = ceil(wp_count_posts() / 3);
$i = 0;
if (have_posts()) :
while(have_posts()) :
$i++;
the_post();
if( ($i % $a_third) === 0 ) :
echo "</div><div class='onethird'>";
endif;
endwhile;
endif;
?>
</div>
我认为您可以使用以下内容:
<?php $i = 0; $dupe = array(); $third = ceil(wp_count_posts() / 3);?>
<?php if (have_posts()) : ?>
<div class="onethird">
<?php while(have_posts()) : the_post(); ?>
<?php if (($i < $third) && (!in_array($post->ID, $dupe))) : ?>
<?php $dupe[] = $post->ID; ?>
<?php echo $post->ID; ?>
<?php get_template_part( 'content', 'category' ); ?>
<?php $i++; ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php $i = 0; rewind_posts(); ?>
<div class="onethird">
<!-- repeat same code as above -->
</div>
<?php $i = 0; rewind_posts(); ?>
<div class="onethird">
<!-- repeat same code as above -->
</div>
<?php endif; ?>