Php 如何防止重复发布?3圈运行
我需要把博客分成三个栏目。为此,我写道:Php 如何防止重复发布?3圈运行,php,wordpress,Php,Wordpress,我需要把博客分成三个栏目。为此,我写道: <?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', 'cate
<?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>
我肯定这是非常糟糕的代码,但这是我能想到的最好的。然后,我注意到重复的post问题。再一次,在谷歌搜索了很多次之后,我错了:
<?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>
现在它只忽略计数器和数组中的,并在所有三列中显示所有帖子
如果有人有更好的解决方案,可以在三列中显示帖子,那也很受欢迎 对于您要实现的目标,三个循环似乎有点复杂
我会得到帖子总数-wp\u count\u posts()
,然后除以3(四舍五入)得到每列的帖子数
然后只要循环一次,只要计数器的剩余部分$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>
三个循环对于您试图实现的目标来说似乎有点复杂
我会得到帖子总数-wp\u count\u posts()
,然后除以3(四舍五入)得到每列的帖子数
然后只要循环一次,只要计数器的剩余部分$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>
我想你可以用你现在拥有的:
<?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; ?>
我认为您可以使用您现有的:
<?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; ?>
您是否有相关链接或示例?这是有道理的,但我认为我自己无法编写这段代码。即使是一些简单的指示,一步一步地解释该做什么也可以。谢谢你有任何链接或一些例子吗?这是有道理的,但我认为我自己无法编写这段代码。即使是一些简单的指示,一步一步地解释该做什么也可以。谢谢我知道你在用复制贴子数组做什么。但是$wp\u query->next\u post()的作用是什么?next_post已被弃用,并替换为next_post_链接,该链接通常用于分页。在调用_post()之前,我也不确定您是否有权访问$post->ID。我不认为你做这件事的方式有什么问题,你只需要通读WP上的多重循环文档就可以了。我理解你在用duplicate posts数组做什么。但是$wp\u query->next\u post()的作用是什么?next_post已被弃用,并替换为next_post_链接,该链接通常用于分页。在调用_post()之前,我也不确定您是否有权访问$post->ID。我不认为你做这件事的方式有任何问题,你只需要通读WP上的多循环文档。