我正在尝试在wordpress中创建多个循环而不重复发布。 这是我现在的代码,我在第一个循环中添加了
<?php $do_not_duplicate = $post->ID?>
我在其余的循环中添加了这个
<?php if( $post->ID == $do_not_duplicate ) continue; ?>
但仍然显示重复的帖子,
请你帮我 感谢这里是所有代码
<div id="main-content" class="main-content">
<!-- -------------------------1----------------------------- -->
<!--4 post de categoria news & what we do -->
<?php query_posts('cat=8,6,3,2&posts_per_page=4&orderby=rand'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- contenido duplicado excluido loop principal-->
<?php $do_not_duplicate = $post->ID; ?>
<?php the_post_thumbnail(); ?>
<!-- <h1><?php the_title() ;?></h1> -->
<!-- <?php the_excerpt(); ?> -->
<!-- <span id="mas">Leer más</span> -->
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
<!-- -------------------------2----------------------------- -->
<!--1 post de categoria video reciente -->
<?php rewind_posts(); ?>
<?php query_posts('category_name=articulo&showposts=1&post_type=post&orderby=date'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- contenido duplicado excluido loop principal-->
<?php if( $post->ID == $do_not_duplicate ) continue; ?>
<?php the_post_thumbnail(); ?>
<!-- <h1><?php the_title() ;?></h1> -->
<!-- <?php the_excerpt(); ?> -->
<!-- <span id="mas">Leer más</span> -->
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
<!-- -------------------------3----------------------------- -->
<!--4 post de categoria news & what we do -->
<?php rewind_posts(); ?>
<?php query_posts('cat=8,6,3,2&posts_per_page=4&orderby=rand'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- contenido duplicado excluido loop principal-->
<?php if( $post->ID == $do_not_duplicate ) continue; ?>
<?php the_post_thumbnail(); ?>
<!-- <h1><?php the_title() ;?></h1> -->
<!-- <?php the_excerpt(); ?> -->
<!-- <span id="mas">Leer más</span> -->
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
<!-- -------------------------4----------------------------- -->
<!--4 post de random de todo -->
<?php rewind_posts(); ?>
<?php query_posts('cat=8,6,3,2,1,4,5,9&posts_per_page=100&orderby=rand'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- contenido duplicado excluido loop principal-->
<?php if( $post->ID == $do_not_duplicate ) continue; ?>
<?php the_post_thumbnail(); ?>
<!-- <h1><?php the_title() ;?></h1> -->
<!-- <?php the_excerpt(); ?> -->
<!-- <span id="mas">Leer más</span> -->
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
答案 0 :(得分:0)
尝试使用以下代码。基本思想是存储显示在数组中的post id。当您处于页面上的任何其他循环时,请检查数组中是否存在该ID。如果id存在,则意味着我们不必显示帖子。
<?php query_posts('cat=8,6,3,2&posts_per_page=4&orderby=rand'); ?>
<?php $ids_done = array(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- contenido duplicado excluido loop principal-->
$current_post_id = get_the_ID();
if( !in_array($current_post_id,$ids_done) ){ // display only if not a repeated post
<?php the_post_thumbnail(); ?>
<!-- <h1><?php the_title() ;?></h1> -->
<!-- <?php the_excerpt(); ?> -->
<!-- <span id="mas">Leer más</span> -->
}
<?php $ids_done[] = $current_post_id; ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>