2013-01-31 27 views
1

我需要将博客分成三列。为此,我写道:如何防止重复的帖子? 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,并显示在所有三列的所有帖子。

如果有人有更好的解决方案来显示三列中的帖子,那也是值得欢迎的!

+0

我明白你有重复的帖子阵列做什么。但$ wp_query-> next_post()是什么? next_post已被弃用,并被替换为通常用于分页的next_post_link。 – Jeff

+0

我也不确定在调用the_post()之前是否有权访问$ post-> ID。我不认为你做这件事的方式有什么问题,你只需要阅读WP上的多个循环文档。 – Jeff

回答

1

三个循环看起来有点复杂,你试图实现。

我会得到帖子的总数 - wp_count_posts() - 并除以3(舍入)以获得每列的帖子数量。

然后你只需要循环一旦你添加</div><div class='onethird'>时,您的计数器$i的每列的职位数所得的余数为0

喜欢的东西:

<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> 
+0

你有任何链接或一些例子吗?这是有道理的,但我不认为我能够自己编写代码。即使是一些简单的指针,也可以逐步解释要做什么。谢谢! – NamanyayG

+0

@Namanyayg我加了一个例子 – jeroen

+0

谢谢了,很好用! :d – NamanyayG

0

我想你可以用你所拥有的这项工作:

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