我需要将博客分成三列。为此,我写道:如何防止重复的帖子? 3个循环运行
<?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_query-> next_post()是什么? next_post已被弃用,并被替换为通常用于分页的next_post_link。 – Jeff
我也不确定在调用the_post()之前是否有权访问$ post-> ID。我不认为你做这件事的方式有什么问题,你只需要阅读WP上的多个循环文档。 – Jeff